I need help with this script for a food in my game. The idea of the script is that it gives the player a speed boost for a few moments and then it finishes but it doesn't seem to do that for my script:
local Tool = script.Parent; Enabled = true script.Parent.Equipped:Connect(function(Mouse) Mouse.Button1Down:Connect(function() if not Enabled then return end Enabled = false local h = Tool.Parent:FindFirstChild("Humanoid") h.WalkSpeed = 50 wait(1) h.walkSpeed = 16 end) end) script.Parent.Unequipped:Connect(function() end)
Than you.
There are some small mistakes. The first one is that .Equipped returns the tool as an argument, not the mouse. Another one is that on the second "WalkSpeed" you wrote "walkSpeed". And I also overall optimized it.
local player = game.Players.LocalPlayer local tool = script.Parent local mouse = player:GetMouse() local enabled = false tool.Equipped:Connect(function(tool) mouse.Button1Down:Connect(function() if enabled then return end enabled = true local humanoid = player.Character.Humanoid humanoid.WalkSpeed = 50 wait(1) humanoid.WalkSpeed = 16 enabled = false end) end)
Well, that didn't work I'll show you my scripts:
WELDING:
local Tool = script.Parent; Enabled = true function onActivated() if not Enabled then return end Enabled = false wait(1) script.Parent.Handle.EatingSound:Play() wait(2) local h = Tool.Parent:FindFirstChild("Humanoid") if (h ~= nil) then if (h.MaxHealth > h.Health + 10) then h.Health = h.Health + 10 else h.Health = h.MaxHealth end end wait(1) script.Parent:remove() end function onEquipped() script.Parent.Handle.OpenSound:play() script.Parent:remove() end script.Parent.Activated:connect(onActivated) script.Parent.Equipped:connect(onEquipped)
SPEED BOOST:
local Tool = script.Parent local db = false script.Parent.Activated:Connect(function() local h = Tool.Parent:FindFirstChild("Humanoid") if db == false then -- Debounce statement so you can't eat the food while still under its effects db = true h.WalkSpeed = 50 wait(10) h.WalkSpeed = 16 db = false end end)
GUI:
Enabled = true script.Parent.Equipped:Connect(function(Mouse) Mouse.Button1Down:Connect(function() if not Enabled then return end Enabled = false animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation) animation:Play() wait(5) script.Parent:Destroy() end) end) script.Parent.Unequipped:Connect(function() animation:Stop() end)
There's a .Activated event for a tool that fires when the mouse is clicked while the tool is equipped. It's much less convoluted and more effective than what you were doing before.
local Tool = script.Parent local db = false script.Parent.Activated:Connect(function() local h = Tool.Parent:FindFirstChild("Humanoid") if db == false then -- Debounce statement so you can't eat the food while still under its effects db = true h.WalkSpeed = 50 wait(1) h.WalkSpeed = 16 db = false end end)