Point reset on death

So what I have are points that the player gains here and there. The points are Int and Num values stored in PlayerGui, so that I can display to the player their points easier.

My issue is that the points return to 0 when the player dies, when I want them to remain the same.
I’m wondering if I should move the point values to somewhere other than on the player (so that the server holds the values) or is it something I have to save and then call upon when the player respawns.

3 Likes

LocalScript:


local Players = game:GetService("Players")

local Player = Players.LocalPlayer
local Char = Player.Character
local humanoid = Char:FindFirstChild("Humanoid")
local Rep = Game:GetService("ReplicatedStorage")
	
humanoid.Died:Connect(function()
    Rep.RemoteEvent:FireServer()
end)

Server Script:

local Rep = Game:GetService("ReplicatedStorage")
local Folder = "GuiName" --if points is not set in a gui, then leave it.
local Points = "points"

Rep.RemoteEvent.OnServerEvent:Connect(function(player)
    local playergui = player:FindFirstChild("PlayerGui")
    local gui = playergui:FindFirstChild(Folder)
    if gui ~= nil then
        local pointsval = gui:FindFirstChild(Points)
        if pointsval ~= nil then
            pointsval.Value = 0
        end
    else
        local pointsval = playergui:FindFirstChild(Points)
        if pointsval ~= nil then
            pointsval.Value = 0
        end
    end
end)

Warning: Not tested!

2 Likes

Actually nevermind I see a problem
You put a point value in player gui which these gui will create a new child and replace last one everytimes you die so just move your point val to player itself instead of playergui

I think i have fixed it
30chars!

Well yeah but explain them abit would be better beside his issue is not about reset his point when he die he wants a point to be saved when he die

Oh, then he must set the values in a folder, or in the player:

Game > Players > LocalPlayer > Points
Or
Game > Players > LocalPlayer > Folder > Points
@Studder_Lleventyate

1 Like

I forgot to say something: Maybe there’s a property in the Gui (ResetOnSpawn) which you must disable, maybe that’s the cause of points reseting!

But that would also save his gui

You should try something else, instead of storing the points in PlayerGui.

My idea is that you should make a folder that stores values for players’ points, and make it in an easy to find place, so you don’t have much issue with coding.

Example server-side script;

game.Players.PlayerAdded:Connect(function(player)
    	local storage = Instance.new("Folder",game.ReplicatedStorage["YOUR DATASTORE FOLDER"])
    	storage.Name = player.Name
    	local points = Instance.new("IntValue", storage)
    	points.Value = 0
    end)

The directory to find the points would be;

local player = game.Players.LocalPlayer
local points = game.ReplicatedStorage["YOUR DATASTORE FOLDER"][tostring(player)]

Hope that helps!

Yeah, I know, but will save his stats

The reason why this is happening is because you have the ResetOnSpawn properly set to true in your ScreenGui. When this property is true all the contents within that ScreenGui will go back to their original state when you die. If you turn that property to false then it should solve your issue.

As a word of warning you should avoid saving values directly from the client because an exploiter could have modified them. To fix this you should keep a log of all the players data on the server and only have the server update it. This way you will always have a ‘correct’ version of the players data.

There is no real reason to use a custom gui unless it is essential. A leaderstats points value can be created easily and it also doesn’t reset on death.

A way to make this using a script is:

game.Players.PlayerAdded:Connect(function(plr)
   local lstat = Instance.new("Folder")
   lstat.Name = "leaderstats"
   lstat.Parent = plr
   
   local points = Instance.new("IntValue")
   points.Name = "Points"
   points.Parent = lstat
end)

This will be stored on the client, though, meaning that you shouldn’t trust the client’s local version of the points value. This is not a problem if you have FilteringEnabled on, which prevents client changes from being replicated onto the server.
A cached value on the server in a table is still recommended.

Never trust the client

If you are storing values in the client that are meant to replicate to the server:

  • Player lags out and crashes, but is dead to the server and other players. Therefore the event is never fired and that player keeps their points

  • An exploiter simply just stops this event from passing through.

It is strongly suggested not to be storing values in the client unless they are insignificant, that is a huge security flaw. Store the values in Player.leaderstats, or in ServerStorage and use a RemoteEvent to replicate the values to that player.

3 Likes

I totally agree with this but you don’t need to use a RemoteEvent to replicate the values to a client.

The server has access to the player at any time (excluding PlayerScripts) and can just change the value when needed.

You need a remote event to replicate values stored in server storage is what I meant.

Solution about this problem is to just create a ServerScript that detect whenever player joined and then create Point value and parent them to the Player instead of Ordinary strategy. I guess?

Hey, this post would be better suited to the Scripting Support category.

The points value should definitely not be stored on the player object, and should be kept on the server. Otherwise, the player can edit their own points incredibly easily and open up an exploit in your game.
The reason the points are disappearing would appear to be because they are in the PlayerGui, which is reloaded from StarterGui when a player character respawns.