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

A nil Value how can i fix it got anybody an idea?

Asked by 5 years ago

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)
0
change plr.leaderstats.Kills.Value to Kills.Value dark_nineret -25 — 5y
0
That won't work, it's outside of the PlayerAdded scope. Ziffixture 6913 — 5y

2 answers

Log in to vote
1
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

Players Service & PlayerRemoving.


The PlayerAdded and PlayerRemoving Signals are both apart of thePlayers` 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)
0
Nice typo you got there: PayerRemoving DeceptiveCaster 3761 — 5y
0
Haha, thanks for pointing that out:) Ziffixture 6913 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited by User#24403 5 years ago

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!

0
This still wont work Ziffixture 6913 — 5y
0
oh oke nvm :( FlabbyBoiii 81 — 5y
0
But it was a good try, learn from it! read my answer above why this won't work Ziffixture 6913 — 5y

Answer this question