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

How do I stop a function when a player unequips his/her tool?

Asked by 7 years ago

This tool makes the player noclip when they equip the tool. I don't know I would stop these functions when the player unequips the tool because there are RunService and on change functions. When I unequip the tool, the script would still run because it's in a loop.

wait(2)
nam = game.Players.LocalPlayer.Name

local tool = script.Parent

local function onEquip()
game:GetService('RunService').Stepped:connect(function()
workspace[nam].Torso.CanCollide = false
workspace[nam].Head.CanCollide = false
end)

workspace[nam].Torso.Changed:connect(function()
workspace[nam].Torso.CanCollide = false
workspace[nam].Head.CanCollide = false
end)
for a,b in pairs(workspace[nam]:GetChildren()) do
    if b:FindFirstChild("Handle") then
        b.Handle.CanCollide = false
    end
end
end

local function onUnequip()
--???
end

tool.Equipped:connect(onEquip)
tool.Unequipped:connect(onUnequip)

2 answers

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

This answer is only to be used and is intended to be used as a reference.

The best, and (maybe) only way to accomplish this is to use the Disconnect function.

What it does is it, well, disconnects a function, or in simpler terms, cancels all execution of an event(s).

Here's an example of how the Disconnect function works in action:

game.Workspace.ChildAdded:connect(FUNCTIONNAME)
-- This event, or function(?) is set to fire whenever a object is added into a specific parent, and if you use a function w/ an argument, it will return the object. (You're required to give the function name for the event to call upon it.)

But there isn't any disconnection; it's just a plain old script.

Oh yeah...

Connection = game.Workspace.ChildAdded:connect(FUNCTIONNAME)
-- We, or I, have set up a variable which is set to the event: this is required to cancel out the connection-to-object-thing.

Connection:Disconnect()
-- Now when you attempt to have the ChildAdded event fire, it wont work anymore. ;)

Literally, it's that simple. lol

But that's not a full script-

Enough Jr. Don't be picky now. (Jkjk lol)

Stuff touched on, but didn't go into detail about

If you wish to check out the WIKI Docu, click on the highlighted words below. :)

The Disconnect Function - A function in which cancels out, stops (future) execution of event, or put simply "disconnects" an event: However, this is only intended for event; attempting to use this for anything else will result in an error.

The ChildAdded Function - An event of an, or any, object w/in the server, and when, say, a part is parented into the said object, it will fire the event. (Click here for more info on Events)

Events - You'll have to go to the WIKI Docu for this, as I don't know a way to explain this. ;-;

Hope this helped you in any way! :D

Ad
Log in to vote
-1
Answered by 7 years ago

Easy, here ya go

wait(2)
nam = game.Players.LocalPlayer.Name

local tool = script.Parent
local Events = {}

local function onEquip()
Events["Noclip"] = 
game:GetService('RunService').Stepped:connect(function()
    workspace[nam].Torso.CanCollide = false
    workspace[nam].Head.CanCollide = false
end)

Events["NoclipChanged"] = workspace[nam].Torso.Changed:connect(function()
workspace[nam].Torso.CanCollide = false
workspace[nam].Head.CanCollide = false
end)
for a,b in pairs(workspace[nam]:GetChildren()) do
    if b:FindFirstChild("Handle") then
        b.Handle.CanCollide = false
    end
end
end

local function onUnequip()
    if Events["Noclip"] and Events["NoclipChanged"] then
        Events["Noclip"]:disconnect()
        Events["NoclipChanged"]:disconnect()
        Events["Noclip"] = nil
        Events["NoclipChanged"] = nil
    end
end

tool.Equipped:connect(onEquip)
tool.Unequipped:connect(onUnequip)

Answer this question