Advertisement
TheQP

Weapon System Script

Jul 19th, 2024
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.23 KB | Source Code | 0 0
  1. -- Weapon System Script
  2.  
  3. -- Setup:
  4. -- 1. Create Weapon Models:
  5. --    - Create models for each weapon type (e.g., Sword, Bow) and place them in ReplicatedStorage.
  6. -- 2. GUI Elements:
  7. --    - Create a ScreenGui in StarterGui.
  8. --    - Inside the ScreenGui, create a Frame for the weapon selector (WeaponFrame).
  9. --    - Inside the Frame, create buttons for each weapon type (SwordButton, BowButton).
  10. -- 3. Script Components:
  11. --    - Place this LocalScript inside the ScreenGui to handle weapon switching and abilities.
  12.  
  13. local player = game.Players.LocalPlayer
  14. local character = player.Character or player.CharacterAdded:Wait()
  15. local humanoid = character:WaitForChild("Humanoid")
  16. local replicatedStorage = game:GetService("ReplicatedStorage")
  17.  
  18. -- GUI Elements
  19. local weaponFrame = player:WaitForChild("PlayerGui"):WaitForChild("ScreenGui"):WaitForChild("WeaponFrame")
  20. local swordButton = weaponFrame:WaitForChild("SwordButton")
  21. local bowButton = weaponFrame:WaitForChild("BowButton")
  22.  
  23. local currentWeapon = nil
  24.  
  25. -- Function to equip a weapon
  26. local function equipWeapon(weaponName)
  27.     if currentWeapon then
  28.         currentWeapon:Destroy()
  29.     end
  30.  
  31.     local weapon = replicatedStorage:FindFirstChild(weaponName):Clone()
  32.     weapon.Parent = character
  33.     weapon:SetPrimaryPartCFrame(character.HumanoidRootPart.CFrame)
  34.     currentWeapon = weapon
  35. end
  36.  
  37. -- Function to use the weapon's special ability
  38. local function useSpecialAbility()
  39.     if currentWeapon and currentWeapon.Name == "Sword" then
  40.         print("Sword Special Ability: Heavy Slash")
  41.         -- Implement heavy slash ability
  42.     elseif currentWeapon and currentWeapon.Name == "Bow" then
  43.         print("Bow Special Ability: Explosive Arrow")
  44.         -- Implement explosive arrow ability
  45.     end
  46. end
  47.  
  48. -- Button connections
  49. swordButton.MouseButton1Click:Connect(function()
  50.     equipWeapon("Sword")
  51. end)
  52.  
  53. bowButton.MouseButton1Click:Connect(function()
  54.     equipWeapon("Bow")
  55. end)
  56.  
  57. -- Bind special ability to a key
  58. local userInputService = game:GetService("UserInputService")
  59.  
  60. userInputService.InputBegan:Connect(function(input, gameProcessed)
  61.     if gameProcessed then return end
  62.  
  63.     if input.KeyCode == Enum.KeyCode.E then
  64.         useSpecialAbility()
  65.     end
  66. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement