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

How can I disconnect this function in a server script?

Asked by 4 years ago
Edited 4 years ago

I swear I was able to disconnect functions before but I seem to have forgotten. I have it so that when the player holds G, it fires a remote event which then activates the function, and when they release G it fires another remote event which is supposed to disconnect the function. I'm having a little trouble with that part. At first I tried this:

01function bandaging(plr)
02    local humanoid = plr.Character:WaitForChild("Humanoid")
03    local wounds = humanoid.Wounds
04    while wait() do
05        if humanoid.Health > 0 then
06            if wounds.Value > 0 then
07                wounds.Value = wounds.Value - 1
08            else
09                return
10            end
11        else
12            return
13        end
14    end
15end
16 
17Bandaging.OnServerEvent:Connect(bandaging)
18 
19CancelBandaging.OnServerEvent:Disconnect(bandaging)

And then I got an error on the last line saying I couldn't use Disconnect I think. So then I tried changing it to lowercase and then got an error saying it was deprecated. My final attempt was this:

01function bandaging(plr)
02    local humanoid = plr.Character:WaitForChild("Humanoid")
03    local wounds = humanoid.Wounds
04    while wait() do
05        if humanoid.Health > 0 then
06            if wounds.Value > 0 then
07                wounds.Value = wounds.Value - 1
08            else
09                return
10            end
11        else
12            return
13        end
14    end
15end
View all 23 lines...

And that also didn't work. It said I was trying to index function with Disconnect. Any help is appreciated.

1 answer

Log in to vote
1
Answered by
nievadev 112
4 years ago

:Connect method returns the connection of the event you just did. That way, you can do :Disconnect on it.

1local connection = Bandaging.OnServerEvent:Connect(bandaging)
2 
3connection:Disconnect()

Please, accept the answer if is it helpful for you! I'm glad to help :)

Ad

Answer this question