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

How would I be able to save these objects?

Asked by
DollorLua 235 Moderation Voter
4 years ago

Ive been making a game like Miners Haven but im stuck on this part, I have no idea what the best way to save and load data so the game knows how many Stone Droppers a player has or how many furnaces a player has. How would I do this. I have made a game like this but I made 50+ Int Values, 1 for each object. Please tell me in the most helpful way, a way thats more efficient.

0
Store the important information, like position relative to XSize/2 and ZSize/2 (the corner), the identifier, the item count inside, and all that fancy stuff. Then when loaded, you can position the objects using nodes or some other means of item generation, and set the data when the object is loaded. Fifkee 2017 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Hello, 100000000Dollorman.I will demonstrate how to use data stores to save values for your game:

Firstly, insert a Script inside ServerScriptService. Then insert the following line:

local DSS = game:GetService("DataStoreService")

We have to get the DataStoreService before we can call it. So we created a variable called DSS, short for DataStoreService, which is what we want, something short to reference, instead of having to say game:GetService("DataStoreService") every single time.

local DSS = game:GetService("DataStoreService")

game.Players.PlayerAdded:Connect(function(Player)
    Player.CharacterAdded:Connect(function(Character)

    end)
end)

Next, we insert this block of code, which runs when the Player and Player's Character are added. Basically when someone first joins the game, it will run whatever code we put inside it and it will happen for every player

local DSS = game:GetService("DataStoreService")

game.Players.PlayerAdded:Connect(function(Player)
    Player.CharacterAdded:Connect(function(Character)
        local Folder = Instance.new("Folder")
        Folder.Name = "Data"
        Folder.Parent = Player
    end)
end)

We are creating a new Folder object, naming it Data (where we will store our data values), and placing it inside the Player. Now we will instance/create an IntValue, call it Cash, set the default value to 0, and place it inside the Data folder.

local DSS = game:GetService("DataStoreService")

game.Players.PlayerAdded:Connect(function(Player)
    Player.CharacterAdded:Connect(function(Character)
        local Folder = Instance.new("Folder")
        Folder.Name = "Data"
        Folder.Parent = Player
        local Cash = Instance.new("IntValue")
        Cash.Name = "Cash"
        Cash.Parent = Player.Data
        Cash.Value = 0
    end)
end)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

*Now we're going to go back near the top of the script under where we made the DSS variable and create the Data Store for our Cash*:

local DSS = game:GetService("DataStoreService")

local PlayerCash = DSS:GetDataStore("Cash")

game.Players.PlayerAdded:Connect(function(Player) Player.CharacterAdded:Connect(function(Character) local Folder = Instance.new("Folder") Folder.Name = "Data" Folder.Parent = Player local Cash = Instance.new("IntValue") Cash.Name = "Cash" Cash.Parent = Player.Data Cash.Value = 0 end) end)


*We just created a Data Store, but we still need to make it load and save. You can change the variable PlayerCash and the "Cash" text inside the quotation marks. Now on to loading the Data Store whenever the Player joins the game*:

local DSS = game:GetService("DataStoreService")

local PlayerCash = DSS:GetDataStore("Cash")

game.Players.PlayerAdded:Connect(function(Player) Player.CharacterAdded:Connect(function(Character) local Folder = Instance.new("Folder") Folder.Name = "Data" Folder.Parent = Player local Cash = Instance.new("IntValue") Cash.Name = "Cash" Cash.Parent = Player.Data Cash.Value = PlayerCash:GetAsync(Player.userId) or 0 end) end)


*Now where it sets our Cash value to 0 by default, we will make it load our Cash OR set it to 0 by default. If the player has no save, it will load what they had before, otherwise start at 0 (or whatever you prefer). Now on to saving*:

local DSS = game:GetService("DataStoreService")

local PlayerCash = DSS:GetDataStore("Cash")

game.Players.PlayerAdded:Connect(function(Player) Player.CharacterAdded:Connect(function(Character) local Folder = Instance.new("Folder") Folder.Name = "Data" Folder.Parent = Player local Cash = Instance.new("IntValue") Cash.Name = "Cash" Cash.Parent = Player.Data Cash.Value = PlayerCash:GetAsync(Player.userId) or 0

    game.Players.PlayerRemoving:connect(function(Player)

    end)

*First, we're going to add another function at the bottom (inside the player/character added function) which will automatically run right before the Player leaves the game and save our Cash. Then*

local DSS = game:GetService("DataStoreService")

local PlayerCash = DSS:GetDataStore("Cash")

game.Players.PlayerAdded:Connect(function(Player) Player.CharacterAdded:Connect(function(Character) local Folder = Instance.new("Folder") Folder.Name = "Data" Folder.Parent = Player local Cash = Instance.new("IntValue") Cash.Name = "Cash" Cash.Parent = Player.Data Cash.Value = PlayerCash:GetAsync(Player.userId) or 0

    game.Players.PlayerRemoving:connect(function(Player)
        PlayerCash:SetAsync(Player.userId, Cash.Value)
    end)
end)

end)


*You successfully created a value to save, and made it save/load whenever the player joins/leaves. Congratulations! Additionally, if you want your data to save automatically every set interval, you can do this*:

local DSS = game:GetService("DataStoreService")

local PlayerCash = DSS:GetDataStore("Cash")

game.Players.PlayerAdded:Connect(function(Player) Player.CharacterAdded:Connect(function(Character) local Folder = Instance.new("Folder") Folder.Name = "Data" Folder.Parent = Player local Cash = Instance.new("IntValue") Cash.Name = "Cash" Cash.Parent = Player.Data Cash.Value = PlayerCash:GetAsync(Player.userId) or 0

    game.Players.PlayerRemoving:connect(function(Player)
        PlayerCash:SetAsync(Player.userId, Cash.Value)
    end)

    while true do
        wait(300)
        PlayerCash:SetAsync(Player.userId, Cash.Value)
    end
end)

end) ~~~~~~~~~~~~~~~~~

Replace the number inside the wait() with the time you want (in seconds) in-between each auto save. Note: There are limits to how frequently you can make data store requests (link here for specific limitations), so do not auto save too fast.

To test the Data Store, test run the game in Studio, then manually change the value of your Cash in your Player. Stop the test run after changing the value yourself, then test run again to see that it saved what you changed it to. That's it for this tutorial, hope it helped!

0
If this not solved your problem get a look at this link: https://developer.roblox.com/en-us/articles/Saving-Player-Data JpcPcGamer360 0 — 4y
Ad

Answer this question