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

How do I stop a script when I equip a tool?

Asked by 4 years ago
Edited 4 years ago

So I have made a string of code that tells my char to stop a script when I equip a tool then turn the script on when I unequip the tool. Here's my code

local Tool = script.Parent.Parent

function onEquippedLocal()
    script.Parent.Parent.Parent:FindFirstChild("Sprint").Disabled = true
end
function onUnequippedLocal()
    script.Parent.Parent.Parent:FindFirstChild("Sprint").Disabled = false
end

Tool.Equipped:connect(onEquippedLocal)
Tool.Unequipped:connect(onUnequippedLocal)

It displays this in the output when I unequip the tool

 20:24:07.228 - Players.Zendex32.Backpack.Tool.Main.LocalScript:7: attempt to index a nil value
20:24:07.229 - Stack Begin
20:24:07.229 - Script 'Players.Zendex32.Backpack.Tool.Main.LocalScript', Line 7
20:24:07.230 - Stack End

The bit for disabling the script works, but when you unequip the tool it doesn't turn the script back on. If someone can fix the script for me that would really help!!!!

Edit: I forgot to say this before but I have tried everything, making a local variable or ANY type of variable BUT NOTHING WORKS!!!!

DOUBLE EDIT: I FIXED IT BY MAKING A VARIABLE HERES THE FIXED SCRIPT

local Tool = script.Parent.Parent
local Plr = script.Parent.Parent.Parent

function onEquippedLocal()
    Plr:FindFirstChild("Sprint").Disabled = true
end
function onUnequippedLocal()
    Plr:FindFirstChild("Sprint").Disabled = false
end

Tool.Equipped:connect(onEquippedLocal)
Tool.Unequipped:connect(onUnequippedLocal)

Feel free to take the script what I did for the tool is put the LocalScript in ANOTHER LocalScript so the Tool isn't so cluttered.

0
The error is saying that the sprint script doesn't exist when you disable it. Check if there is any other tool script that might be causing this problem when the tool is equipped. RayCurse 1518 — 4y
2
When the Tool is equipped I am quite sure it is moved from the player's Backpack to their Character. Although I am not sure if your "Equipped function" is run before or after the Tool is moved. But the tool is at different locations when your different functions run. Spjureeedd 385 — 4y
0
That is probably why. Post this as an answer. RayCurse 1518 — 4y
0
Nothing works as I have said I have tried everything. Zendex32 -2 — 4y
View all comments (3 more)
0
also, I recommend using more variables because those .Parent.Parent.Parent chains are quite messy RiskoZoSlovenska 378 — 4y
0
I suggest using "WaitForChild" because it cannot find it yet, probably because it hasn't loaded. Slatryte 104 — 4y
0
@RiskoZoSlovenska You were right! That did fix it. Zendex32 -2 — 4y

1 answer

Log in to vote
0
Answered by
haba_nero 386 Moderation Voter
4 years ago

Here is a neat fact about scripts. I saw you used a sprint script so you can sprint when holding the tool.

In order to make the character sprint, you need a SCRIPT. I am assuming you used a LOCALSCRIPT for the tool.

Instead of disabling a script using a local script, just use this NORMAL SCRIPT DIRECTLY in the tool.

script.Parent.Equipped:Connect(function()
    local Player = script.Parent.Parent:FindFirstChild("Humanoid")
    if Player.Health then
        Player.WalkSpeed = 32
    end
end
script.Parent.Unequipped:Connect(function()
    wait(0.1)
    local Player = script.Parent.Parent.Character:FindFirstChild("Humanoid")
    if Player.Health then
        Player.WalkSpeed = 16
    end
end

This should do what I assumed you wanted to do.

If you have any questions or errors, comment below!

-TheLastHabanero

0
You clearly did NOT read the script I made, the script was for turning OFF the sprint script also this did not work. Zendex32 -2 — 4y
0
Wait. So you want to disable the sprint forever? haba_nero 386 — 4y
0
No I exactly said; I want the script to disable ONLY while I'm holding out the tool. Zendex32 -2 — 4y
0
Oh. Just swap the Equipped and Unequipped statements then :/ haba_nero 386 — 4y
Ad

Answer this question