Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Set leaderstat "Objects" to 0 when player dies?

Asked by 5 years ago

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?

0
You're using the assigned to operator. Use the assignment operator (=) Rare_tendo 3000 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

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
0
I dont want to save the stats,only set them to 0 when player dies SpringtraPlayer 16 — 5y
0
Ok but just saying in case you want to save the amount of objects Tymberlejk 143 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

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)
0
(on localscript) FrezeTagger 75 — 5y

Answer this question