How do u make this script only run when a tool has activated and touched the part

this script is a script inside the part:

local debounce = false
local player = game:GetService(“StarterPack”).Speed

script.Parent.Touched:Connect(function(hit)
if hit.Name == “RightHand” then
if hit.Parent:FindFirstChild(“Humanoid”) then
if debounce == false then
local player = game.Players:FindFirstChild(hit.Parent.Name)
player.leaderstats.Speed.Value = player.leaderstats.Speed.Value + 10
debounce = true
wait(3)
debounce = false
end
end
end
end)

2 Likes
local activated = false

script.Parent.Activated:Connect(function()
	activated = true
end)

script.Parent.Deactivated:Connect(function()
	activated = false
end)

Then add a check for “activated” inside the function connected to the “.Touched” event.

2 Likes

my script is in the part so what do i do?

Change “script.Parent” so that the script correctly references the tool.

call the function with the event: tool.Activated:Connect()

after that, use :GetTouchingParts() to find out if the part is touching the other part. If it does, run your code.

it isnt working and btw this script is in the part