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

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

Asked by 3 years ago

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

Log in to vote
-1
Answered by 3 years ago
Edited 3 years ago

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!

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. tightanfall 110 — 3y
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! sne_123456 439 — 3y
0
If you could, I would like to see your alternative for research purposes! :) tightanfall 110 — 3y
0
ok ill fix it later sne_123456 439 — 3y
Ad

Answer this question