Hello! I’m trying to add a Stats GUI, though I am having trouble with it. It works when I join the game, but when I die and respawn, it doesn’t work. I don’t want to disable it resetting when you die, due to it breaking a Debounce I have set, so I don’t want to mess with that.
Here is the code I made, look specifically at the CharacterAdded event as that’s where it is not working.
--Services
local DSS = game:GetService('DataStoreService')
local SDS = DSS:GetDataStore('StatsDataStore')
local RS = game:GetService('ReplicatedStorage')
--Remote Events
local StatsData = RS.RemoteEvents.Data.StatsData
game.Players.PlayerAdded:Connect(function(player)
local StatsFolder = Instance.new('Folder')
StatsFolder.Name = 'StatsFolder'
StatsFolder.Parent = player
local Strength = Instance.new('IntValue')
Strength.Name = 'Strength'
Strength.Parent = StatsFolder
local playerUserId = player.UserId
local Strength = player.StatsFolder.Strength
local data
local success, errMsg = pcall(function()
data = SDS:GetAsync(playerUserId..'-strength')
end)
if success then
Strength.Value = data
end
player.CharacterAdded:Connect(function(char)
local PlayerGUI = player.PlayerGui
local StatsStrength = PlayerGUI:WaitForChild('StatsGUI').StatsFrame.StatsStrength
StatsStrength.Text = 'Strength : '..Strength.Value
Strength.Changed:Connect(function()
local playerUserId = player.UserId
local Strength = player.StatsFolder.Strength
local data = Strength.Value
local success, errMsg = pcall(function()
SDS:SetAsync(playerUserId..'-strength',data)
end)
if success then
StatsStrength.Text = 'Strength : '..Strength.Value
end
end)
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = player.UserId
local Strength = player.StatsFolder.Strength
local data = Strength.Value
local success, errMsg = pcall(function()
SDS:SetAsync(playerUserId..'-strength',data)
end)
end)
If you notice any problems with my code and what can be fixed, tell me.