What do you want to achieve? Save Players Health when leaving
What is the issue? uhhh i tried making it work by myself but i cant, so i guess asking to other devs will help… so the issue is i made a script which syncs the players health (Humanoid.Health) with the int value i made, so in game it syncs well but when i leave the intvalue didnt saved, i tried making a function which if the player left, the script (that syncs the humanoid health and int value) disables so hopefully the value of the intvalue remains but it didnt work, and i have a data store, the max health that increases when leveled up works fine, and it remains the same whenever i join again. i also make the data load when a player joined so i guess thats not the problem.
What solutions have you tried so far? I didnt noticed this question but i already said the solutions i tried, and ye i tried to find a similar issue with mine but it works differently.
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
The Script (local script) that syncs with the humanoid health and intvalue
local Player = game.Players.LocalPlayer
local char = Player.Character
char:WaitForChild("Humanoid").HealthChanged:Connect(function()
Player:WaitForChild("PlayerStatus").Health.Value = char:WaitForChild("Humanoid").Health
end)
game.Players.PlayerRemoving:Connect(function()
script.Disabled = true
end)
Incase you need my entire player status data store system
local DataStore = game:GetService("DataStoreService")
local PlayerData = DataStore:GetDataStore("test123")
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function()
local UserId = Player.UserId
local Data = PlayerData:GetAsync(UserId)
local PlayerStatus = Instance.new("Folder", Player)
PlayerStatus.Name = "PlayerStatus"
local Speed = Instance.new("IntValue", PlayerStatus)
Speed.Name = "Speed"
Speed.Value = 12
local MaxHealth = Instance.new("IntValue", PlayerStatus)
MaxHealth.Name = "MaxHealth"
MaxHealth.Value = 100
local Health = Instance.new("IntValue", PlayerStatus)
Health.Name = 'Health'
Health.Value = 100
if Data then
Speed.Value = Data["Speed"]
MaxHealth.Value = Data["MaxHealth"]
Health.Value = Data["Health"]
else
Speed.Value = 12
MaxHealth.Value = 100
Health.Value = 100
end
end)
end)
local function CreateTable(player)
local PlayerStatus1 = {}
for i, stat in pairs(player:WaitForChild("PlayerStatus"):GetChildren()) do
PlayerStatus1[stat.Name] = stat.Value
end
return PlayerStatus1
end
local function PlayerLeave(player)
local PlayerStatus1 = CreateTable(player)
local success, err = pcall(function()
local UserId = player.UserId
PlayerData:SetAsync(UserId, PlayerStatus1)
end)
if not success then
warn("data cant be found")
end
end
game.Players.PlayerRemoving:Connect(PlayerLeave)
Uhhh hope you can understand my english
(I just need a way to make the health save (IntValue) and that will help, no need to make an entire script)
You’re updating the value of the Humanoid on the client. The server won’t see any changes therefore your script won’t work properly.
You’re disabling the script after PlayerRemoving fires. The local script will disable itself when another player leaves the game.
You’re not using a pcall for GetAsync. If DataStore:GetAsync fails, the player won’t receive the required objects to play the game.
You’re using the parent argument for Instance.new.
You’re not using game:BindToClose. This crucial if you want your data saving system to work correctly.
The way you receive your data should look like this:
local Data
local success, output = pcall(PlayerData.GetAsync, UserId)
-- safely gets the player's data
if not success then
warn("Unable to find data:", output)
-- tells you that it was unable to find the player's data and the reason why
else
Data = output
-- or you can just use the output variable for the rest of your script
end
DataModel:BindToClose fires when the server closes completely (when the last player leaves the server, the server crashes, etc). It should look like this:
game:BindToClose(function()
for _, plr in pairs(Players:GetPlayers()) do
-- loops through all the players in the game
-- creates a separate thread for them to run so you can save all of their
-- data individually
coroutine.wrap(PlayerLeave)(plr)
end
end)
For your local script, do you damage the player locally? Or does a server script damage them? If it’s the latter, you should just detect humanoid health changes on the server. If it’s the former, you should change it to the latter (unless your system requires it to be on the client).
Oh thanks, but the the purpose of the local script i made is to sync the IntValue with the health of the humanoid, so if the humanoid health is 30 then the intvalue is also 30, im not making the intvalue be the humanoid’s health, i tried making a script (server script) that does this but it dont work, so i made it a local script instead.
And for the damage, i made a fall damage for the mean time and its inside of a server script
(and it works fine, sometimes not cause of my high ping)
Ill try to fix my code as i can (not that experienced on scripting)
Based on some researchs an int value that was changed locally will not saved, so i recreated the script version of it and it looks like this
HealthSyncer (Server Script)
local character = script.Parent
local hum = character:WaitForChild("Humanoid")
local player = game.Players:GetPlayerFromCharacter(character)
hum.HealthChanged:Connect(function()
player:WaitForChild("PlayerStatus").Health.Value = hum.Health
end)
So i have data store for it (which i changed slightly based on HugeCoolboy2007’s suggestions)
and a server script inside the server script service which loads the saved intvalue (Health of the player) on the first time they join
game.Players.PlayerAdded:Connect(function(player)
local died = false
local Joined = true
player.CharacterAdded:Connect(function(Character)
local Hum = Character:WaitForChild("Humanoid")
local Healthstatus = player:WaitForChild("PlayerStatus").Health.Value
local MaxHealthstatus = player:WaitForChild("PlayerStatus").MaxHealth.Value
Hum.Died:Connect(function()
died = true
end)
if Joined then
wait(5)
player.Character.Humanoid.Health = Healthstatus
print("WELCOME")
Joined = false
end
if died and not Joined then
wait(2)
player.Character.Humanoid.Health = MaxHealthstatus
print("OH")
died = false
end
end)
end)
So i guess here is the solution if your int value wont saved