Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What exactly does my script do? (BindToClose,IsStudio)

So i'm been making this Inventory script and I'm using datastore to save tables. I've noticed that sometimes my datastore hasn't been saving, and I can't figure out how to convert it into datastore two. So to my solution I used the function bindtoclose. It was suggested by Incapaz and saves 100% of the time. Although I've been getting a lot of bugs on my inventory concerning duplication whenever the player rejoins. I feel like if I could get a better understanding on this then maybe I can fix the bug. Any help will be apprecated.

Server Script

--This Saves the Inventory
local function OnPlayerRemoving(Player)
    for i,v in pairs(Player.InventoryFolder.Inventory:GetChildren()) do
    	table.insert(itemlist, {v.Name, v.Value})
	end
		Datastore:SetAsync(Player.UserId, itemlist)
		print("Saved Inventory")
end

--Then I put it through the BindToClose function with a bunch of other things
local function OnGameShutdown()
    if not RunService:IsStudio() then
		local players = Players:GetPlayers()
		for _, player in pairs(players) do
			OnPlayerRemoving(player)--This is where a run the save script inside the bindtoclose function
		end
		while true do
			wait()
		end
	else
		wait(5)
	end
end

game.Players.PlayerRemoving:Connect(OnPlayerRemoving)
game:BindToClose(OnGameShutdown)

Regards, Bl_ueHistory

1 answer

✓ Accepted answer
local itemlist = {} --// Table to hold items

local function OnPlayerRemoving(Player) --// Function with the name of "OnPlayerRemoving" with one argument of "Player"
    
    for i,v in pairs(Player.InventoryFolder.Inventory:GetChildren()) do --// This will loop through all of the children through of the Player's InventoryFolder's Inventory, the object will be "v" and the item number in the list will be "i". I read it as "i" is int (number) and "v" for value as in the value of the object.
        
        table.insert(itemlist, {v.Name, v.Value}) --// This will insert a table into the table containing the name and the value of the object into the table named "itemlist", but you'll have to create the table first.
        
    end --// Closes the loop
    
    Datastore:SetAsync(Player.UserId, itemlist) --// This will save the itemlist table to the datastore with the scope of their userId
    
    print("Saved Inventory") --// Prints "Saved Inventory" in the output of the game
    
end --// Closes the function


local function OnGameShutdown() --// Function with the name of "OnGameShutdown" with not arguments
    
    if not RunService:IsStudio() then --// This will check if the script is being ran in studio or not. If it is then it will wait 5 seconds, if it's in a live server, it'll continue down the code.
        
        local players = Players:GetPlayers() --// Returns a table of all players existing in the game at that moment
        
        for _, player in pairs(players) do --// Loops through all the players in the table
            
            OnPlayerRemoving(player) --// Uses the previous created function to save the player's data
            
        end --// Closes loop
        
        while true do --// A while loop, not sure for what, but repeats wait() for eternity.
            
            wait() --// Waits 0.03 seconds
            
        end --// Closes while loop, even though this will never end
        
    else --// Other code that will run if it is studio

    wait(5) --// Will just wait 5 seconds and do nothing
    
    end --// Closes if then statement
    
end --// Closes function

game.Players.PlayerRemoving:Connect(OnPlayerRemoving) --// (Fires whenever a player leaves the game) Saves the player's data when they leave the game
game:BindToClose(OnGameShutdown) --// (Fires whenever the game shutsdown the server) Saves all the player's data when the game shutsdown (updates or unexpectedly)

Hope this helped! All the answers are in the comments!

This discussion is preserved as it was. New answers, comments, and votes are closed.