The script works just fine but I am new to scripting and wondering if anyone has some pointers to make my script more efficient and less space consuming. -Thanks in advance
local Part = script.Parent.Head Part.Touched:connect(function(hit) local player = game.Players:GetPlayerFromCharacter(hit.Parent) local chara = workspace:FindFirstChild(player.Name) if hit.Parent:FindFirstChild("Humanoid") then if chara:FindFirstChild("SpeedTag") then else if hit.Parent.Humanoid.Health > 0 then local hum = hit.Parent.Humanoid hum.WalkSpeed = hum.WalkSpeed + 50 local speedtag = Instance.new("BoolValue") speedtag.Name = "SpeedTag" speedtag.Parent = workspace:FindFirstChild(player.Name) end end end end)
(The script takes whoever stands on it and adds speed if they havent already stood on it, adds a boolvalue to indicate that they have stepped on it. And if they try to step on it again, nothing happens due to the character having the bool in it.)
-P.S. it needs to add speed instead of setting it because I plan to add other speed boosters so they can stack.
Hi, if you want to reduce lines, i suggest that instead of creating a bool value using instance.new, you just set a bool value for a variable.
For example:
local Part = script.Parent.Head local speedTag = false Part.Touched:connect(function(hit) local player = game.Players:GetPlayerFromCharacter(hit.Parent) local chara = workspace:FindFirstChild(player.Name) if hit.Parent:FindFirstChild("Humanoid") then if speedTag == false then if hit.Parent.Humanoid.Health > 0 then local hum = hit.Parent.Humanoid hum.WalkSpeed = hum.WalkSpeed + 50 speedTag = true end end end end)
This works a lot like debounce, except that there is no cooldown time. You also don't need the unnecessary else and an if statement the other way around.
Hope this was helpful!
Any questions? Just ask!