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.
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)
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
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)]
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.
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.
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.
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?
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.