Player not given WalkSpeed upon dying

Hello, I am making a button where if you have 30 coins, you can click it to buy it, remove 30 coins from your leaderstats, then give walkspeed.
Right now, I am trying to make it so it gives you the speed again when you reset.
But for some reason, it does not work at all. I am not given the speed. Anyone know why?

I have:
leaderstats (in player), Coins (IntValue)
Stats (in player), hasBoughtSpeed (bool value, automatically set to false upon join)

And also, the script sets the bool value to true when the person has 30 or more coins and has bought it, but it doesn’t set to true for some reason.

Local Script inside of TextButton:


local player = game.Players.LocalPlayer

local button = script.Parent

local originalPrice = 30
local originalSpeed = 21

local hasBought = player:WaitForChild("Stats").hasBoughtSpeed.Value

button.MouseButton1Click:Connect(function()
	pcall(function()
		if player.leaderstats then
			if player.leaderstats.Coins.Value >= originalPrice then
				if hasBought == false then
					hasBought = true
					player.leaderstats.Coins.Value -= originalPrice
					if hasBought == true then
						player.Character:WaitForChild("Humanoid").WalkSpeed = originalSpeed
					end
				end
			else
				warn("player doesn't have enough coins!")

			end

		end
	end)
end)

Thank you!

1 Like

There’s nothing detecting respawns, also, why are you using pcall?

I would connect an event like so:

local RespawnConnection = nil

local function GiveRespawnConnection()
    if not RespawnConnection then
        RespawnConnection = player.CharacterAdded:Connect(function(character)
            local humanoid = character:WaitForChild("Humanoid", 10)
            humanoid.WalkSpeed = originalSpeed
        end)
    end
end

Call this when the player purchases it:

button.MouseButton1Click:Connect(function()
	if player.leaderstats then
		if player.leaderstats.Coins.Value >= originalPrice then
			if hasBought == false then
				hasBought = true
				player.leaderstats.Coins.Value -= originalPrice
				player.Character:WaitForChild("Humanoid").WalkSpeed = originalSpeed
				GiveRespawnConnection()
			end
		else
			warn("player doesn't have enough coins!")
		end
	end
end)

Full code:

local player = game.Players.LocalPlayer

local button = script.Parent

local originalPrice = 30
local originalSpeed = 21

local hasBought = player:WaitForChild("Stats").hasBoughtSpeed.Value

local RespawnConnection = nil

local function GiveRespawnConnection()
    if not RespawnConnection then
        RespawnConnection = player.CharacterAdded:Connect(function(character)
            local humanoid = character:WaitForChild("Humanoid", 10)
            humanoid.WalkSpeed = originalSpeed
        end)
    end
end

button.MouseButton1Click:Connect(function()
	if player.leaderstats then
		if player.leaderstats.Coins.Value >= originalPrice then
			if hasBought == false then
				hasBought = true
				player.leaderstats.Coins.Value -= originalPrice
				player.Character:WaitForChild("Humanoid").WalkSpeed = originalSpeed
				GiveRespawnConnection()
			end
		else
			warn("player doesn't have enough coins!")
		end
	end
end)

Hope this helps!

1 Like

You need to change the value on the server, since you are doing it on the client, it will only work on the client. Try using a remoteevent.

1 Like

The speed changing on the client works fine, I do this in all my projects, it’s faster and there’s no reason to change it on the server.

1 Like

It still doesn’t give me the speed again upon reseting, but I did notice this in the output:

RunService:UnbindFromRenderStep removed different functions with same reference name utility-focus-state-inspect-Triallss 2 times

This appeared when I reseted. Maybe this is the problem?

1 Like

No, but if you have data saving then thats why.

Add this:

if hasBought then
	GiveRespawnConnection()
end
1 Like

It’s really weird but it STILL doesn’t work…
Maybe I have to add a RemoteEvent, then change the values to the server?

1 Like

I figured it out! I had to use a RemoteEvent to change the values and stuff to the Server, because changing the Coins value in the client and changing the HasBought bool in the client wouldn’t work!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.