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

My dataStore won't work, and my leaderstats is broken?

so, i have been trying to make a simulator in roblox by watching alvinblox's vid, and i typed half of the script (not fully done with the script) and he said for me i am supposed to test it to see if the dataStore works or not but when i test it in studio it doesnt work (leaderstats works in studio) but when i test in the main game the leaderstats doesnt work plus i see no error in the console.

Script:

local serverStorage = game:GetService("ServerStorage") local DataStore = game:GetService("DataStoreService"):GetDataStore("PlayerSave3")

game.Players.PlayerAdded:Connect(function(player)

local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player

local strength = Instance.new("NumberValue") strength.Name = "Strength" strength.Parent = leaderstats

local rebirths = Instance.new("IntValue") rebirths.Name = "Rebirths" rebirths.Parent = leaderstats

local dataFolder = Instance.new("Folder") dataFolder.Name = player.Name dataFolder.Parent = serverStorage.RemoteData

local debounce = Instance.new("BoolValue") debounce.Name = "Debounce" debounce.Parent = dataFolder

local strengthData, rebirthsData

local success,errormessage = pcall(function() strengthData = DataStore:GetAsync("strength-"..player.UserId) rebirthsData = DataStore:GetAsync("rebirths-"..player.UserId) end)

if success then if strengthData then strength.Value = strengthData rebirths.Value = rebirthsData end

end

end)

game.Players.PlayerRemoving:Connect(function(player) local success, errormessage = pcall(function() DataStore:SetAsync("strength-"..player.UserId,player.leaderstats.Strength.Value) DataStore:SetAsync("rebirths-"..player.UserId,player.leaderstats.Rebirths.Value) end) end)

4 comments

0

can you please put your code in a code box?

0

i am new here, how do i do that?

0

look at evaera's test question

1 answer

The obvious mistakes


The visible mistakes in your code are that

  • you are using 3 periods (...) to concatenate strings, rather than 2 periods (..), which is the proper way of concatenating .
  • you are overriding a previous save by saving strength, then rebirths on the same datastore

A couple of extra suggestions


  • save dictionaries to a single datastore, as opposed to saving multiple numbers on different datastores.
  • Don't use Alvinblox tutorials as your primary avenue of learning, as his tutorials does have a problem with spoonfeeding the viewer code without much in the way of educating
  • Use BindToClose to make sure every player's data is saved when a server shuts down

Application


With that said, here is an improved version of that specific part of your script

--server/normal script
local serverStorage = game:GetService("ServerStorage")  
local DataStore = game:GetService("DataStoreService"):GetDataStore("PlayerSave3")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new(“Folder”)  
	leaderstats.Name  = "leaderstats" 
	leaderstats.Parent = player

	local strength = Instance.new(“NumberValue”)  
	strength.Name  = "Strength"
	strength.Parent = leaderstats

	local rebirths = Instance.new(“IntValue”)  
	rebirths.Name  = "Rebirths"
	rebirths.Parent = leaderstats

	local DataTable = {}

	local success,errormessage = pcall(function()  
		DataTable = DataStore:GetAsync(player.UserId)  
	end)

	if success and DataTable then 
		strength.Value = DataTable.Strength 
		rebirths.Value = DataTable.Rebirths
	end
end)

local function savedata (player)
	local ls = player.leaderstats
	local saveTable = {Strength = ls.Strength.Value, Rebirths = ls.Rebirths.Value}
	local success, error = pcall(function()  
		DataStore:SetAsync(player.UserId,saveTable)  
	end)
end)

game.Players.PlayerRemoving:Connect(savedata)

game:BindToClose(function()
	for _,player in pairs(game.Players:GetPlayers()) do
		savedata(player)
	end
end)

Note that i used a savedata function in order to not repeat myself with both the playerRemoving and BindToClose functions

Appologies for any capitalization errors that I did not catch

Hopefully this helped! Be sure to accept if it did!

1 comment

0

Actually he does explain how the code works , his videos have helped me understand remote functions and events and other things

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