I am trying to make a Script that adds Speed and then brings it back to its default

Issue here though,

What i want to do is that the speed stacks up when stepped on another “part”.
Basically you start at 55 WalkSpeed
The Pad adds 45 WalkSpeed Makes it 100, but it should go back to 55 after time.

When stepped on another one the speed is 145 but should also go back to the default speed at 55.

I am very new to scripting and don’t even know many of the basics.

This is my script!
Thank you for helping! :slight_smile:

local part = script.Parent

local function onTouch(otherPart)
	local humanoid = otherPart.Parent:FindFirstChild("Humanoid")
	if humanoid then
		wait(0.01)
		script.Parent.Color = Color3.fromRGB(255, 0, 4)
		script.Parent.Emission.Enabled = true
		script.Parent.Attachment5.PointLight.Range = 40
		wait(0.01)
		otherPart.Parent.Humanoid.WalkSpeed += 45
		wait(0.6)
		script.Parent.Emission.Enabled = false
		wait(0.01)
		script.Parent.Attachment5.PointLight.Range = 36
		otherPart.Parent.Humanoid.WalkSpeed = - 5
		wait(0.4)
		otherPart.Parent.Humanoid.WalkSpeed = - 5
		script.Parent.Attachment5.PointLight.Range = 33
		wait(0.3)
		otherPart.Parent.Humanoid.WalkSpeed = - 5
		wait(0.2)
		otherPart.Parent.Humanoid.WalkSpeed = - 5
		script.Parent.Attachment5.PointLight.Range = 30
		wait(0.1)
		otherPart.Parent.Humanoid.WalkSpeed = - 5
		wait(0.1)
		otherPart.Parent.Humanoid.WalkSpeed = - 5
		wait(0.1)
		otherPart.Parent.Humanoid.WalkSpeed = - 5
		script.Parent.Attachment5.PointLight.Range = 28
		wait(0.1)
		otherPart.Parent.Humanoid.WalkSpeed = - 5
		script.Parent.Attachment5.PointLight.Range = 24
		wait(0.1)
		otherPart.Parent.Humanoid.WalkSpeed = - 5
		script.Parent.Color = Color3.fromRGB(173, 173, 14)
	end
end

part.Touched:Connect(onTouch)
2 Likes

I am somewhat confused about your problem. Is the problem the speed boost stacking? Is the problem about the speed boost not returning to normal?

1 Like

it’s both, so i am very confused, when i make it stackable, the returning doesnt work
if i make the returning work, the stacking doesnt work then

I don’t have a solution for the speed boost not returning to normal yet, but what I suggest might solve both problems at once.

You should detect if a player has already received a speed boost. This can be done in a few different ways. You can add a value inside the player, or store the values in the script.

local function onTouch(otherPart)
	local humanoid = otherPart.Parent:FindFirstChild("Humanoid")
	if humanoid then
		if not ReceivedSpeedBoost then --the "ReceivedSpeedBoost" variable can be changed or you could check the player's WalkSpeed
			--code to handle giving speed boost and etc if needed
		end
	end
end

If i were to approach this issue, I’d add a localscript to startercharacterscripts and use the for i,v do loop to get every pad. Then, add a touched function. To get it globally, you’ll then use a script in serverscriptservice and add a remote event in replicatedstorage.

Similarly, like what @ Archenhailor said, you could add a value inside the player as a debounce. However, I’ll prob use a table for this.

localscript:

local debounce = {}

for i, pad in workspace.Pads:GetChildren() do -- folder in workspace called Pads with all the part/pad inside.
	if pad:IsA("Part") then
		pad.Touched:Connect(function(hit) --touched function
			local hum = hit.Parent:FindFirstChildOfClass("Humanoid")
			if hum and hum.Parent.Name == game.Players.LocalPlayer.Name then -- checks if local player touches a pad
				if not table.find(debounce, pad) then -- debounce
					table.insert(debounce, pad)
					game.ReplicatedStorage.RemoteEvent:FireServer()
					wait(3)
					table.remove(debounce, table.find(debounce,pad))
					end
				end
		end)
	end
end

Then, in script you could get the player’s walkspeed and do:

local event = game.ReplicatedStorage.RemoteEvent

event.OnServerEvent:Connect(function(plr)
	plr.Character.Humanoid.WalkSpeed += 45
	wait(3) -- how long the boost last
	plr.Character.Humanoid.WalkSpeed -= 45
end)
3 Likes