quhluh

roblox sword fight game code useful

Mar 26th, 2023
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. local sword = game.Workspace.Sword -- Assumes the sword is a child of the Workspace object
  2. local player = game.Players.LocalPlayer -- Gets the local player object
  3.  
  4. local swordSwingCooldown = 0.5 -- How long the player has to wait between swings
  5. local swordSwingTime = 0.1 -- How long the sword takes to swing
  6.  
  7. local function onSwing()
  8. if sword.CanCollide then -- Only allow the player to swing if the sword is not already colliding with something
  9. sword.CanCollide = false -- Set the sword to be non-collidable so it can pass through other objects
  10. wait(swordSwingTime) -- Wait for the sword to finish swinging
  11. sword.CanCollide = true -- Set the sword back to collidable so it can hit other objects
  12. end
  13. end
  14.  
  15. local function onKeyPress(inputObject, gameProcessedEvent)
  16. if gameProcessedEvent then return end -- Only register the key press if it hasn't already been processed by another script
  17.  
  18. if inputObject.KeyCode == Enum.KeyCode.MouseButton1 then -- Check if the left mouse button was pressed
  19. if tick() - (lastSwingTime or 0) > swordSwingCooldown then -- Check if the player has waited long enough to swing again
  20. lastSwingTime = tick() -- Record the time of the last swing
  21. onSwing() -- Call the onSwing function to swing the sword
  22. end
  23. end
  24. end
  25.  
  26. player:GetMouse().KeyDown:connect(onKeyPress)
Advertisement
Add Comment
Please, Sign In to add comment