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

Why does my Datastore save Data only once?

Here's my script:

local DSS = game:GetService("DataStoreService")
local myDSS = DSS:GetDataStore("Store1")


game.Players.PlayerAdded:connect(function(player)
local stats = Instance.new("Folder")
stats.Name = "Stats"
stats.Parent = player

local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Parent = stats

local Gems = Instance.new("IntValue")
Gems.Name = "Gems"
Gems.Parent = stats

local Damage = Instance.new("IntValue")
Damage.Name = "Damage"
Damage.Parent = stats

local Speed = Instance.new("IntValue")
Speed.Name = "Speed"
Speed.Parent = stats

local Health = Instance.new("IntValue")
Health.Name = "Health"
Health.Parent = stats


local playerUserId = player.UserId

local data
local success, errormessage = pcall(function()
data = myDSS:GetAsync(playerUserId)
end)

if success then
	if data
	then
		Coins.Value = data[1]
        Gems.Value = data[2]
        Damage.Value = data[3]
        Health.Value = data[4]
        Speed.Value = data[5]
        print(player.Name.." has Coins, Gems, Damage, Health, Speed: "..table.concat(data, ","))
	end

end

end)

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

local playerUserId = player.UserId

local data = {player.Stats.Coins.Value,player.Stats.Gems.Value, player.Stats.Damage.Value,player.Stats.Health.Value,player.Stats.Speed.Value}



local success, errormessage = pcall(function()
myDSS:SetAsync(playerUserId, data)
end)

if success then
print("Data Saved Success!")
else
print ("There was an error!")
warn(errormessage)
end
end)

game.Workspace.Moneypart.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid")
	then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent) 
        player.Stats.Gems.Value = player.Stats.Gems.Value +1
	end
	
end)

Thanks in advance!

2 comments

1

u should put your saving script in a function and loop it with while wait(45)do

1

Use "game:BindToClose()" to save all the player's data when the game gets closed.

1 answer

Hello.


You'd need to use the game:BindToClose() function and then save everyone's data.

game:BindToClose() binds a function to be called prior to the game shutting down. (Developer shuts down all servers, etc.)

To do this, you must get all the players in the game, and then save their data.

Also, you must use an if statement checking if you are in-studio, as the DataStores will complete in offline testing sessions if you're in-studio. If you are in studio, it will stop the BindToClose() and close the game.

Here's your script:

local DSS = game:GetService("DataStoreService")
local myDSS = DSS:GetDataStore("Store1")


game.Players.PlayerAdded:connect(function(player)
local stats = Instance.new("Folder")
stats.Name = "Stats"
stats.Parent = player

local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Parent = stats

local Gems = Instance.new("IntValue")
Gems.Name = "Gems"
Gems.Parent = stats

local Damage = Instance.new("IntValue")
Damage.Name = "Damage"
Damage.Parent = stats

local Speed = Instance.new("IntValue")
Speed.Name = "Speed"
Speed.Parent = stats

local Health = Instance.new("IntValue")
Health.Name = "Health"
Health.Parent = stats


local playerUserId = player.UserId

local data
local success, errormessage = pcall(function()
data = myDSS:GetAsync(playerUserId)
end)

if success then
    if data
    then
        Coins.Value = data[1]
        Gems.Value = data[2]
        Damage.Value = data[3]
        Health.Value = data[4]
        Speed.Value = data[5]
        print(player.Name.." has Coins, Gems, Damage, Health, Speed: "..table.concat(data, ","))
    end

end

end)

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

local playerUserId = player.UserId

local data = {player.Stats.Coins.Value,player.Stats.Gems.Value, player.Stats.Damage.Value,player.Stats.Health.Value,player.Stats.Speed.Value}



local success, errormessage = pcall(function()
myDSS:SetAsync(playerUserId, data)
end)

if success then
print("Data Saved Success!")
else
print ("There was an error!")
warn(errormessage)
end
end)

game:BindToClose(function()

	if game:GetService("RunService"):IsStudio() then 
		return 
	end

	local players = game:GetService("Players"):GetPlayers()

	for _, player in pairs(players) do
		local data = {
			player.Stats.Coins.Value,
			player.Stats.Gems.Value, 
			player.Stats.Damage.Value,
			player.Stats.Health.Value,
			player.Stats.Speed.Value
		}

		local success, errormessage = pcall(function()
			myDSS:SetAsync(player.UserId, data)
		end

		if success then
			print("Data Saved Success!")
		else
			print ("There was an error!")
			warn(errormessage)
		end
	end
end)

game.Workspace.Moneypart.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid")
    then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent) 
        player.Stats.Gems.Value = player.Stats.Gems.Value +1
    end

end)


I hope this helps. Cheers!

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