Need help with changing player speed

This is my script its a normal script its do too many tings but there is a problem in changing player speed so if player speed +16 the script change the player seed to 20

This is the script

local Players = game:GetService("Players")
local Part = script.Parent
local Debounce = true
local Fire = game.ReplicatedStorage.Fire

Part.Touched:Connect(function(hit)
	local Player = Players:GetPlayerFromCharacter(hit.Parent)

	if Player and Debounce then
		Debounce = not Debounce

		local Leaderstats = Player:WaitForChild("leaderstats")
		local Leaderstats2 = Player:WaitForChild("leaderstats2")
		local Dif = Leaderstats2:WaitForChild("Dif")
		local char = Player.Character or Player.CharacterAdded:Wait()
		local hum = char:WaitForChild("Humanoid")
		local speed = hum.WalkSpeed
		local Clone = workspace.RoadModel:Clone()
		local NewPosition = CFrame.new(workspace.RoadModel.CloneTo.Position)

		Clone:SetPrimaryPartCFrame(NewPosition)
		Clone.Parent = workspace	
		Dif.Value += 1
		Player.Character.Humanoid.WalkSpeed += 1
		if Dif.Value >= 5 then
		Dif.Value = 5
		end		
		if speed >= 16 then
			speed = 20
			print("Speed")	
		end
		task.wait(1)
		Part.Parent:Destroy()
		workspace.Obstacles:Destroy()

		local Obstacles = Instance.new("Folder")
		Obstacles.Name = "Obstacles"
		Obstacles.Parent = workspace
		
	    for _ = 1,Dif.Value do
		
		local Laser = game.ReplicatedStorage.MainLaser
		local Clone1 = Laser:Clone()
		local Road1 = game.Workspace.RoadModel.Right
		local boundsX1 = math.random(-Road1.Size.X/2, Road1.Size.X/2)

		Clone1.Position = (Road1.Position + Vector3.new(boundsX1,Road1.Position.Y))

		Clone1.Parent = workspace.Obstacles	



		local Road2 = game.Workspace.RoadModel.Mid	
		local boundsX2 = math.random(-Road2.Size.X/2, Road2.Size.X/2)	
		local Clone2 = Laser:Clone()

		Clone2.Position = (Road2.Position + Vector3.new(boundsX2,Road2.Position.Y))

		Clone2.Parent = workspace.Obstacles	



		local Road3 = game.Workspace.RoadModel.Left
		local boundsX3 = math.random(-Road3.Size.X/2, Road3.Size.X/2)
		local Clone3 = Laser:Clone()

		Clone3.Position = (Road3.Position + Vector3.new(boundsX3,Road3.Position.Y))

		Clone3.Parent = workspace.Obstacles
			
			
---------------------------------Fire
		
		
local Road1 = game.Workspace.RoadModel.Right
local boundsX1 = math.random(-Road1.Size.X/2, Road1.Size.X/2)
local Clone4 = Fire:Clone()
		
Clone4.Position = (Road1.Position + Vector3.new(boundsX1,Road1.Position.Y))

Clone4.Parent = workspace.Obstacles	
	
	
	
local Road2 = game.Workspace.RoadModel.Mid	
local boundsX2 = math.random(-Road2.Size.X/2, Road2.Size.X/2)	
local Clone5 = Fire:Clone()

Clone5.Position = (Road2.Position + Vector3.new(boundsX2,Road2.Position.Y))

Clone5.Parent = workspace.Obstacles	
	
	
	
local Road3 = game.Workspace.RoadModel.Left
local boundsX3 = math.random(-Road3.Size.X/2, Road3.Size.X/2)
local Clone6 = Fire:Clone()

Clone6.Position = (Road3.Position + Vector3.new(boundsX3,Road3.Position.Y))

Clone6.Parent = workspace.Obstacles					
			
			
				print("Done")
				
		end
	end	
end)

and this is the part that there is an issue in it

if speed >= 16 then
			speed = 20
			print("Speed")	
		end

the print work but the player speed did not changed

1 Like

Are there any errors? Iā€™m gonna guess not

no there is no errors (Extra Characters)

The speed variable is only saving the value of the humanoid walkspeed once, therefore when you change it, it only changes the variable itself and not the humanoid walkspeed.
If you want to change the speed, you need to do

if hum.WalkSpeed >= 16 then
    hum.WalkSpeed = 20
end
1 Like

Did this work?

You could try to print the walkspeed

ty bro (Extra Characters hi hi hi)