Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Weapon Damage script sometimes has a delay?

Asked by 4 years ago

I made a weapon thats not a tool, ie mesh. So the damage script is a bit buggy.

Sometimes it works just fine sometimes it has a delay.

the script is disabled when i copy it it gets enabled by the other script the script is copied to the hitbox and when the hitbox is hit it does damage to the thing that hit it

It has no errors, but it works weirdly

script.Parent.Touched:Connect(function(hit)
    local char = hit.Parent
    local hum = char:FindFirstChild("Humanoid")
    if hum and char.Name ~= script.Parent.Parent.Parent.Name and hum ~= nil then
        hum.Health = hum.Health - script.Dmg.Value
        script.Disabled = true
        wait(0.5)
        script.Disabled = false
    end
end)

2 answers

Log in to vote
0
Answered by 4 years ago

Hello, i am not the best at scripting so if this doesnt work then please dont be mad. Maybe change the 0.5 to 0. I dont think this works but still, give it a try.

0
I see what you are thinking but that would make it fire spontaneously. 123nabilben123 499 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

So the problem is here that you are disabling the script that you need to run. Easy fix.

So a little insight here, the function wait() stalls the script. But we have an event here. Events I think do listen even if the script is stalled by a wait() To do this we disconnect the event.

By the way when events are listening while a wait() is being executed it will fire that amount of times it has listened to. E.g the event listens 7 times while wait() is executed, after that wait() the event will fire 7 times.

This may seem weird but we can make an event a variable. For this we need to connect the event differently.

local function onTouch(hit)
    local char = hit.Parent
    local hum = char:FindFirstChild("Humanoid")
    if hum and char.Name ~= script.Parent.Parent.Parent.Name and hum ~= nil then
        hum.Health = hum.Health - script.Dmg.Value
        TouchEvent:Disconnect()
        wait(0.5)
        TouchEvent = script.Parent.Touched:Connect(onTouch)
    end
end)

TouchEvent = script.Parent.Touched:Connect(onTouch)

I bet you are confused right now but we are saying a reference to the part being touched, if we find a humanoid we will disconnect the event so no more times it can call. Then after .5 seconds we will reconnect and carry on.

Check this roblox wiki link for more on events: https://developer.roblox.com/en-us/articles/events

The reason disabling the script is dangerous as say you are printing some stuff.

print("cats")
script.Disabled = true --Anything past this WILL NOT WORK
script.Disabled = false --not work
print("cats") -- not work
--Why? Cause you disabled the script. Disabling means full on no execution at all. Unless you --Can make another script undisable it.

Answer this question