Hello i get this Message when run this got i tried some things but i dont get it anybody an idea how to fix it? The green line is the problem
14:08:21.857 - ServerScriptService.CurrencyScript1.1:34: attempt to index field 'LocalPlayer' (a nil value)
local DataStore = game:getService( "DataStoreService") local Storage = DataStore:GetDataStore( "SaveSystem") game.Players.PlayerAdded:Connect(function(plr) local folder =Instance.new("Folder") folder.Name = "leaderstats" folder.Parent = plr local Kills = Instance.new("IntValue") Kills.Name = "Kills" Kills.Value = 0 Kills.Parent = folder local cash = Instance.new("IntValue") cash.Name = "Cash" cash.Value = 100 cash.Parent = folder local success, result = pcall(function() Storage:GetAsync(plr.UserId) end) if not success then warn(result) else local LoadTable = Storage:GetAsync(plr.UserId) if LoadTable then Kills.Value = LoadTable[1] cash.Value = LoadTable[2] end end end) -- game.Players.LocalPlayer.PlayersRemoving:Connect(function(plr) local SaveTable = {plr.leaderstats.Kills.Value.plr.leaderstats.Cash.Value} Storage:SetAsync(plr.UserId. SaveTble) end)
The PlayerAdded
and PlayerRemoving Signals are both apart of the
Players` Service. These Signals can only be accessed with the pertaining Service.
Instead, you accidentally called game.Players.LocalPlayer.PlayerRemoving
, which wouldn't work as the LocalPlayer
is a PlayerObject
, not the actual Service required to Listen for these Signals.
You also cannot call the LocalPlayer
from the Server as it's not an actual Client, therefore it doesn't have it's own PlayerObject
, or anything related.
You will also get another error with line 35
. This is because you continued to traverse through the leaderstats
folder looking for something that won't be found by the Script.
Ascending through an Object is done by the .
, you forgot to separate the two desired Elements to upload through the SaveTable
array, which can be done by using a ,
. Without this, it will look for plr
in Kills
' value, which is a big nono.
Revised Code
game.Players.PlayerRemoving:Connect(function(plr) local SaveTable = {plr.leaderstats.Kills.Value,plr.leaderstats.Cash.Value} Storage:SetAsync(plr.UserId, SaveTable) --// You said 'SaveTble', Comma here too for giving it the second argument, and in general. end)
you wrote
game.Players.LocalPlayer.PlayersRemoving:Connect(function(plr) local SaveTable = {plr.leaderstats.Kills.Value.plr.leaderstats.Cash.Value} Storage:SetAsync(plr.UserId. SaveTble) end)
this is the fix
game.Players.PlayerRemoving:Connect(function(plr) local SaveTable = {plr.leaderstats.Kills.Value,plr.leaderstats.Cash.Value} Storage:SetAsync(plr.UserId. SaveTble) end)
the reason your code didn't work is because PlayerRemoving isn't part of the player, it's a member of the Players service where all the players are, as well as that the "LocalPlayer" can't be fetched by a server script.
anyways hope this helped!