I have this upgrades script, and it works as intended (player clicks button, gets stat if player has enough currency) however, when the player leaves and rejoins, it will add stats to the player (refer to video, notice how all stats that have been upgraded at least once will get their values added to when the player rejoins)
https://gyazo.com/eb810e077c889cd7d73d9722864491c0
I know it has something to with the playeradded event but I don’t know how to re format it to work properly
-- Speed
game.Players.PlayerAdded:Connect(function(plr)
local function updateWalkSpeed()
local character = plr.Character
if character then
local humanoid = character:FindFirstChildOfClass("Humanoid")
local upgradesFolder = plr:FindFirstChild("Data") and plr.Data:FindFirstChild("Upgrades")
if humanoid and upgradesFolder then
local speedValue = upgradesFolder:WaitForChild("Speed").Value
if speedValue == 0 then
humanoid.WalkSpeed = 16
else
humanoid.WalkSpeed = 8 * speedValue
end
end
end
end
plr.CharacterAdded:Connect(updateWalkSpeed)
local upgradesFolder = plr:WaitForChild("Data"):WaitForChild("Upgrades")
local speedValue = upgradesFolder:WaitForChild("Speed")
speedValue:GetPropertyChangedSignal("Value"):Connect(updateWalkSpeed)
end)
--Button Cooldown
local function updateLabel(plr)
local label = plr:WaitForChild("Data"):WaitForChild("PlayerData").ButtonCooldown -- change this
local upgradeslabel = plr:WaitForChild("Data"):WaitForChild("Upgrades"):WaitForChild("Button Cooldown") -- change this
local function update()
local upgradesValue = tonumber(upgradeslabel.Value) or 0
if upgradeslabel.Value == 0 then return end
label.Value = label.Value - 0.05 -- * upgradesValue
end
update()
upgradeslabel.Changed:Connect(update)
end
game.Players.PlayerAdded:Connect(function(plr)
updateLabel(plr)
end)
--Rune Cooldown
local function updateRuneLabel(plr)
local label = plr:WaitForChild("Data"):WaitForChild("PlayerData").RuneCooldown -- change this
local upgradeslabel = plr:WaitForChild("Data"):WaitForChild("Upgrades"):WaitForChild("Rune Cooldown") -- change this
local function updateRune()
local upgradesValue = tonumber(upgradeslabel.Value) or 0
if upgradeslabel.Value == 0 then return end
label.Value = label.Value - 0.05 -- * upgradesValue
end
updateRune()
upgradeslabel.Changed:Connect(updateRune)
end
game.Players.PlayerAdded:Connect(function(plr)
updateRuneLabel(plr)
end)
--Luck
local function updateLuckLabel(plr)
local label = plr:WaitForChild("Data"):WaitForChild("PlayerData").Luck -- change this
local upgradeslabel = plr:WaitForChild("Data"):WaitForChild("Upgrades"):WaitForChild("Rune Luck") -- change this
local function updateLuck()
local upgradesValue = tonumber(upgradeslabel.Value) or 0
if upgradeslabel.Value == 0 then return end
label.Value = label.Value + 0.25 -- * upgradesValue
end
updateLuck()
upgradeslabel.Changed:Connect(updateLuck)
end
game.Players.PlayerAdded:Connect(function(plr)
updateLuckLabel(plr)
end)
--Bulk
local function updateBulkLabel(plr)
local label = plr:WaitForChild("Data"):WaitForChild("PlayerData").Bulk -- change this
local upgradeslabel = plr:WaitForChild("Data"):WaitForChild("Upgrades"):WaitForChild("Bulk") -- change this
local function updateBulk()
local upgradesValue = tonumber(upgradeslabel.Value) or 0
if upgradeslabel.Value == 0 then return end
label.Value = label.Value + 1 -- * upgradesValue
end
updateBulk()
upgradeslabel.Changed:Connect(updateBulk)
end
game.Players.PlayerAdded:Connect(function(plr)
updateBulkLabel(plr)
end)