local sword = game.Workspace.Sword -- Assumes the sword is a child of the Workspace object
local player = game.Players.LocalPlayer -- Gets the local player object
local swordSwingCooldown = 0.5 -- How long the player has to wait between swings
local swordSwingTime = 0.1 -- How long the sword takes to swing
local function onSwing()
if sword.CanCollide then -- Only allow the player to swing if the sword is not already colliding with something
sword.CanCollide = false -- Set the sword to be non-collidable so it can pass through other objects
wait(swordSwingTime) -- Wait for the sword to finish swinging
sword.CanCollide = true -- Set the sword back to collidable so it can hit other objects
end
end
local function onKeyPress(inputObject, gameProcessedEvent)
if gameProcessedEvent then return end -- Only register the key press if it hasn't already been processed by another script
if inputObject.KeyCode == Enum.KeyCode.MouseButton1 then -- Check if the left mouse button was pressed
if tick() - (lastSwingTime or 0) > swordSwingCooldown then -- Check if the player has waited long enough to swing again
lastSwingTime = tick() -- Record the time of the last swing
onSwing() -- Call the onSwing function to swing the sword
end
end
end
player:GetMouse().KeyDown:connect(onKeyPress)
Comments