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

Inventory save script with datastores not working?

Asked by 1 year ago

I have been trying to write code which would save the tools in a player's inventory when they leave using datastores, but it doesn't seem to work. I don't know why. Here is my script:

> local players = game:GetService("Players")
> local DataStoreService = game:GetService("DataStoreService")
> local InventoryDatastore1 = DataStoreService:GetDataStore("Datastore1")
> 
> players.PlayerRemoving:Connect(function(player)
>   local inventory = player:WaitForChild("Backpack")
>   local playerUserID = player.UserId
>   local data = inventory:GetChildren()
>   local success, errorMessage = pcall(function()
>       InventoryDatastore1:UpdateAsync("Player_"..playerUserID, data)
>   end)
> 
>   if success then
>       print("Tools successfuly saved!")
>   else
>       print("Something went wrong, "..errorMessage)
>   end
> end)
> players.PlayerAdded:Connect(function(player)
>   local inventory = player:WaitForChild("Backpack")
>   local playerUserID = player.UserId
>   local data
>   local success, errorMessage = pcall(function()
>       data = InventoryDatastore1:GetAsync("Player_"..playerUserID)
>   end)
> end)

When I run my script, the pcall gives me this error message: 16:22:36.241 Something went wrong, Unable to cast value to function - Server

I don't know how to fix this, so if someone could help me that would be great. Help would be appreciated.

Answer this question