Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Button1Down function not looping damage?

When the player clicks down the button and holds it, the damage will continue to happen when they are being clicked over. The problem is, the script only damages the player once


player = game.Players.LocalPlayer
character = player.Character
gun = character:FindFirstChild("Rifle")
mouse = player:GetMouse()
auto = false
function getModel(raw)
    for _, child in pairs(workspace:GetChildren()) do
    --calling pairs is barely any slower than using next directly, plus it's easier to read.
        if raw:IsDescendantOf(child) then
            return child;
        end;
    end;
    return nil; --this allows us to not use pcall
end;

mouse.Button1Down:connect(function()
	auto = true
	while auto do
    if mouse.Target then
        local model = getModel(mouse.Target)
        if model and model:FindFirstChild("Humanoid") then
            model.Humanoid.Health = model.Humanoid.Health - 15
			wait(1)
	end
mouse.Button1Up:connect(function()
	auto = false
end)
        end
    end
end)

2 comments

0

The problem may be that mouse.Target shows the current mouse's target and changes even when the mouse isn't clicked so if you move it off the player it'll stop running.

0

The game freezes at the spot

1 answer

✓ Accepted answer

Crashing means that when you move it off the player's character it'll do a while without a wait because it'll keep checking the if and it'll be false so it won't run the wait inside the if statement. Which while true do without a wait crashes servers.

3 comments

0

how do i fix this issue

0

Move the wait() to the moment the while starts

0

or right before the end

This discussion is preserved as it was. New answers, comments, and votes are closed.