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)
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)
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!