Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Making a script for increasing speed. Any pointers to make this script more efficient?

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.

1 answer

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!

4 comments

0

But if the player dies would that reset the debounce? The reason I had the bool was because when the player dies they lose the bool and can use it again.

0

ah! if you are talking about dying, i could refix the script to do that, but that would just make the script longer than yours. So I would recommend your script!

0

If you could, I would like to see your alternative for research purposes! :)

0

ok ill fix it later

This discussion is preserved as it was. New answers, comments, and votes are closed.