Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Set leaderstat "Objects" to 0 when player dies?

Im using that script

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character
local Stats = Player:WaitForChild("leaderstats")
local Objects = Player.leaderstats.Objects
Objects.Value = 0
Objects.Parent = Stats
Objects.Name = "Objects"

while true do 
	
	if script.Parent.Parent.Parent.Parent.Character.Humanoid.Health = 0 then
		Player.Objects.Value == 0
	end
end

Is a Local script in Starter gui,Anything wrong in the script or its onlt bad placed?

1 comment

0

You're using the assigned to operator. Use the assignment operator (=)

2 answers

I see 2 errors in your script.

1st one:

if script.Parent.Parent.Parent.Parent.Character.Humanoid.Health = 0 then
	Player.Objects.Value == 0
end

You used = when CHECKING if the player's health is 0 and == when SETTING the value of Objects to 0.

= is used to SET a value and == is used to CHECK if a value is equal to another value.

2nd one:

Don't use LocalScripts to save things. Use a Script in ServerScriptStorage instead:

local DataStoreService = game:GetService('DataStoreService')
local DataStore = DataStoreService:GetDataStore('SaveSystem') -- you can name SaveSystem something else

while wait(1) do -- that way it will check every second
	game.Players.PlayerAdded:Connect(function(player)
		local leaderstats = Instance.new('Folder', player)
		leaderstats.Name = "leaderstats"
		local Objects = Instance.new("IntValue", leaderstats)
		Objects.Value = DataStore:GetAsync(player.UserId) or 0 -- it will use the value from the player's data or 0 if joined the first time
		DataStore:SetAsync(player.UserId, money.Value)
		Objects.Changed:Connect(function()
			DataStore:SetAsync(player.UserId, Objects.Value) -- that way it will save Objects whenever it is changed
		end)
	end)
	if player.Character.Humanoid.Health == 0 then
		Objects.Value = 0
	end
end

2 comments

0

I dont want to save the stats,only set them to 0 when player dies

0

Ok but just saying in case you want to save the amount of objects

You can use Died event for your script. For example:

local Player = game:GetService("Players").LocalPlayer

Player.Character.Humanoid.Died:Connect(function()
       --your code
end)

1 comment

0

(on localscript)

This discussion is preserved as it was. New answers, comments, and votes are closed.