#Why is it printing nil?

1 messages · Page 1 of 1 (latest)

sturdy goblet
#
local datastoreService = game:GetService("DataStoreService")
local PlayerTools = datastoreService:GetDataStore("PlayerTools")


game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(character)
        task.wait(10)
        
        print("10 seconds pased")
        
        local suucces, inventory = pcall(function()
            return PlayerTools:GetAsync(plr.UserId)
        end)
        if suucces then
            for _, toolName in inventory do
            print(toolName)
            end
        else
            print("Could not get data")
        end
    end)
end)


game.Players.PlayerRemoving:Connect(function(plr)
    local backpack = plr.Backpack
    local char = plr.Character
    local myTools = {}
    if backpack then
        for _, tool in backpack:GetChildren() do
            if tool:IsA("Tool") then
                table.insert(myTools,tool.Name)
                print(myTools[1])
            end
        end
    end
    if char then
        for _, tool in char:GetChildren() do
            if tool:IsA("Tool") then
                table.insert(myTools, tool.Name )
                print(myTools[tool.Name])
            end
        end
    end
    
    
    local succes, err = pcall(function()
        return PlayerTools:SetAsync(plr.UserId, myTools)
    end)
    if succes then
        print(myTools[1])
    else
        print(err)
    end
end)

when I was simply cloning the tool in character added and set its parent to player backpack it was printing the tool name, but now it prints nil when leaving

#

even when its not, its always nil

boreal sonnet
#

so you save with the tool in your backpack?

#

not equipped?

#

oh wait i didn't see that you loop through the character

#

this would print nil

sturdy goblet
#

dosent matter the scenario, it prints the nil from succes from the pcall down in the player removing

sturdy goblet
boreal sonnet
#

also why are you waiting 10 seconds before loading the data

boreal sonnet
#

what's the output

sturdy goblet
boreal sonnet
sturdy goblet
boreal sonnet
#

yeah

sturdy goblet
#

so ill do a print(mytools) before local succes...

boreal sonnet
#

i looked it up and the character is already destroyed when playerremoving fires

sturdy goblet
#

ill try not equiping the tool before leaving

boreal sonnet
#

you really should have another method of storing an inventory

#

in a table

#

not backpack

sturdy goblet
#

that would be better, but Im a begginer with datastore so I dont know how to work much with them

#

the output with the tool not being equipped

boreal sonnet
#

so you saved the table

#

then it should be loaded

sturdy goblet
#

yeah the character is the problem most likely

#

should I do character remove in player removing?

dry tree
#

It probably means it gets deleted

sturdy goblet
#

plr.CharacterRemoving isnt helping

outer haven
#

Talking about the game doesnt save when you exit studio?
If it so, use game:BindToClose.
There is a chance that, it doesnt trigger onPlayerLeave, if you end it via studio.

sturdy goblet
outer haven
#

No, you should keep playerRemoving, If you stop the server, it trigger game:BindToClose instead playerRemoving.

Let's say, player is playing on your Server, and if you decide to Update the game, it will close the server, which it won't trigger the playerRemoving event.
So you simple use game:BindToClose, Loop all players, and save the data.

sturdy goblet
outer haven
#

Yea kinda, or you just put them into function.