Okay, so I am creating a script in which when you click a button (on a gui), your heath increases. The health increasing, however, is in another script. This is the saving one. I want the health to be the same amount the player upgraded it to after the player respawns. It does work at first, but then I encounter problems (no error in the output though, sadly.)
It will work until I reset. (ex. the health increases by 10 every time you click 'again, the health increases in another script, but that's not the problem.' so in this case, let's say I increased it to 120. After I reset, it will load 120 health and maxhealth. But then, after I RESET (bold that word) if I upgrade the health again and reset again, it will just stay at 120 again, and again, and again.
I have no idea how I'd fix it, I've tried adding a wait after clicking the button, adding a return end at the end of the button clicking, but I just can't find out why it doesn't work once the player respawns.
THINGS TO KNOW:
-All variables are tagged (earlier in the script, this is just the problematic part.)
-This is in a server script located in Workspace.
I know this is long, but I'd appreciate it SO MUCH if you can help! Thanks if you can!!! Here's the script:
player.PlayerGui:WaitForChild("Shop").Frame.Upgrades.Health:WaitForChild("Upgrade").MouseButton1Down:connect(function()
if player.PlayerGui:WaitForChild("Shop").Frame.Upgrades.Health:WaitForChild("Upgrade").Text ~= "UPGRADE" then return end
wait(0.5)
local ds3 = game:GetService("DataStoreService"):GetDataStore("Upgrade_Health")
local price = player.PlayerGui.Shop.Frame.Upgrades.Health.Price
local humanoidz = player.Character.Humanoid
local key = "user_".. player.userId
ds3:UpdateAsync(key, function(oldValue)
local newValue = price.Value or 0
return newValue
end)
local key = "user_" .. player.userId
ds3:UpdateAsync(key, function(oldValue)
local newValue = humanoidz.MaxHealth or 0
return newValue
end)
local key = "user_" .. player.userId
ds3:UpdateAsync(key, function(oldValue)
local newValue = humanoidz.Health or 0
return newValue
end)
return end)
player.CharacterAdded:connect(function(char)
local ds3 = game:GetService("DataStoreService"):GetDataStore("Upgrade_Health")
local price = player.PlayerGui.Shop.Frame.Upgrades.Health.Price
local humanoidz = player.Character.Humanoid
local key = "user_"..player.userId
price.Value = ds3:GetAsync(key, price.Value)
humanoidz.MaxHealth = ds3:GetAsync(key, humanoidz.MaxHealth)
humanoidz.Health = ds3:GetAsync(key, humanoidz.Health)
return end)
|