Change walkspeed on touch

What I want to accomplish: I want to make it so when you touch a part your speed increases by 5

This is my code I currently have that does not work

local part = script.Parent

local function onTouch(otherPart)
	local humanoid = otherPart.Parent:FindFirstChild('Humanoid')
	if humanoid then
		part.Transparency = 1
		part.CanCollide = false
		part.CanTouch = false
		game.StarterPlayer.CharacterWalkSpeed = game.StarterPlayer.CharacterWalkSpeed + 5
	end
end

part.Touched:Connect(onTouch)

This is a script inside of the part

If you want to change walkspeed it has to be done in the player’s humanoid, here is the script but using this fix.

local part = script.Parent

local function onTouch(otherPart)
	local humanoid = otherPart.Parent:FindFirstChild('Humanoid')
	if humanoid then
		part.Transparency = 1
		part.CanCollide = false
		part.CanTouch = false
		humanoid.WalkSpeed = humanoid.WalkSpeed + 5
	end
end

part.Touched:Connect(onTouch)
5 Likes

Also Instead of making the transparency cancollide and cantouch false on the part you can simply just remove it using Destroy() or remove(), here’s the script using this.

local part = script.Parent

local function onTouch(otherPart)
	local humanoid = otherPart.Parent:FindFirstChild('Humanoid')
	if humanoid then
		humanoid.WalkSpeed = humanoid.WalkSpeed + 5
		part:Destroy()
	end
end

part.Touched:Connect(onTouch)
2 Likes

I would destroy it but I want it to respawn after some time

local part = script.Parent

local function onTouch(otherPart)
	local humanoid = otherPart.Parent:FindFirstChild('Humanoid')
	if humanoid then
		part.Transparency = 1
		part.CanCollide = false
		part.CanTouch = false
		humanoid.WalkSpeed += 5
	end
end

part.Touched:Connect(onTouch)
1 Like
local part = script.Parent
local players = game:GetService("Players")

local function onTouch(otherPart)
	if otherPart.Parent:FindFirstChild("Humanoid") then
		local character = otherPart.Parent
		local humanoid = character:WaitForChild("Humanoid")
		local player = players:GetPlayerFromCharacter(character)
		if player then
			if player:FindFirstChild("GotSpeed") then
				return
			end
			humanoid.WalkSpeed += 5
			local addSpeed = Instance.new("BoolValue")
			addSpeed.Name = "GotSpeed"
			addSpeed.Value = true
			addSpeed.Parent = true
		end
	end
end

part.Touched:Connect(onTouch)

players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		if player:FindFirstChild("GotSpeed") then
			local hasSpeed = player:FindFirstChild("GotSpeed")
			if hasSpeed.Value then
				humanoid.WalkSpeed += 5
			end
		end
	end)
end)

This will make it so that touching the part gives the player permanant +5 walkspeed even after they reset. The part isn’t affected as it can only be used once per player (feel free to change that around).

1 Like

Thanks I have one question. How could I add my leaderstats to the change the speed intvalue to +5. This is easy in a local script but I have never done this in a server script. Is their a way? When I try it, it index nil with leaderstats

Sure, this is how you would add leaderstats.

local part = script.Parent

local function onTouch(otherPart)
	local humanoid = otherPart.Parent:FindFirstChild('Humanoid')
	if humanoid then
		part.Transparency = 1
		part.CanCollide = false
		part.CanTouch = false
		humanoid.WalkSpeed = humanoid.WalkSpeed + 5
		
		local leaderstats = game.Players:GetPlayerFromCharacter(otherPart.Parent).leaderstats
		local stat = leaderstats:FindFirstChild("speed") -- change speed to the value name
		if stat ~= nil then stat.Value = stat.Value + 5 end -- ~= nil means if it exists
	end
end

part.Touched:Connect(onTouch)