I made a repeat wait until loop that’s supposed to damage the target until the value is set to “stop”, but even after it’s set to stop, the damage continues.
Here’s my code:
local Event = script.Parent:WaitForChild("RemoteEvent")
Event.OnServerEvent:Connect(function(Player, Type, Mouse)
if Type == "start" then
repeat
wait(1.5)
Mouse.Parent.Humanoid:TakeDamage(2)
until Type == "stop"
end
end)
The problem with your code is that Type only gets evaluated once when the function gets called, and its value doesn’t get updated inside the loop.
local EventStart = Instance.new("BindableEvent")
local EventStop = Instance.new("BindableEvent")
EventStart.Event:Connect(function(Mouse)
repeat
wait(1.5)
Mouse.Parent.Humanoid:TakeDamage(2)
until EventStop.Event:Wait()
end)
script.Parent.RemoteEvent.OnServerEvent:Connect(function(Player, Type, Mouse)
if Type == "start" then
EventStart:Fire(Mouse)
elseif Type == "stop" then
EventStop:Fire()
end
end)
i’ve read the coroutine documentation and i dont see how i would implement it, so i think i’ll try bindable events. I just don’t know where to put the bindable events thing, in the server script?
ive been testing it and it only damages them when i initially press the key and then damages them again when i release the key, but nothing during holding
local Event = script.Parent:WaitForChild("RemoteEvent")
Event.OnServerEvent:Connect(function(Player, Type, Mouse)
if Type == "start" then
local isRunning = true
spawn(function()
repeat
wait(1.5)
Mouse.Parent.Humanoid:TakeDamage(2)
until not isRunning
end)
Event.OnServerEvent:Connect(function(Player, UpdatedType)
if UpdatedType == "stop" then
isRunning = false
end
end)
end
end)