hello i made a datastore script to save a players MaxHealth and WalkSpeed Humanoid properties. It was working fine for awhile so i was working on other datastore scripts that i need for my mmo game then suddenly i noticed that the MaxHealth and WalkSpeed were not saving anymore. I have some print statements in the player removing and added functions and they print the correct values in the removing function but when the player leaves the game and joins back the values are back to 100 maxHealth and 16 walkSpeed(i change the values in game using a skill tree i made that takes talent points to upgrade health and walkspeed and jumppower).
local ds = game:GetService("DataStoreService")
local store2 = ds:GetDataStore("saveMaxHealth")
local store3 = ds:GetDataStore("saveWalkSpeed")
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAppearanceLoaded:Connect(function(character)
local savedMaxHealth
local success,fail = pcall(function()
savedMaxHealth = store2:GetAsync(player.UserId)
end)
if success then
player.Character.Humanoid.MaxHealth = savedMaxHealth
end
if fail then
print("failed to save maxhealth")
end
end)
end)
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAppearanceLoaded:Connect(function(character)
local savedWalkSpeed
local success,fail = pcall(function()
savedWalkSpeed = store3:GetAsync(player.UserId)
end)
if success then
player.Character.Humanoid.WalkSpeed = savedWalkSpeed
end
if fail then
print("failed to save walkspeed")
end
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
if player.Character then
local humanoid = player.Character.Humanoid
store2:SetAsync(player.UserId, humanoid.MaxHealth)
print("player MaxHealth saved: "..tostring(humanoid.MaxHealth))
end
end)
game.Players.PlayerRemoving:Connect(function(player)
if player.Character then
local humanoid = player.Character.Humanoid
store3:SetAsync(player.UserId, humanoid.WalkSpeed)
print("player WalkSpeed saved: "..tostring(humanoid.WalkSpeed))
end
end)
game:BindToClose(function()
wait(5)
end)
Hello!
Try to save the data again in the bindtoclose.
If the problem still remains, remove the wait(5) in there, because the game only finish when the function ends. (Limit of 30 seconds)
There’s a couple problems I see here with this script. I’m assuming this is one script. First of all you should load all the data in one playeradded function.
Second I’m not sure why your using character appearance loaded instead of character added but that shouldn’t matter.
Third and probably most important, when you save these values you’re saving them to the same key (player.UserId). Each key for each value should be different or you should use a table. With the way your doing it though you should use something like (player.UserId… “-Health”) and (player.UserId… “-WalkSpeed”) and use those to load it instead of the same key.
Sorry I didn’t mean to bump I just somehow realized this was from a month ago.