Help me make datasave script

65 until 69

uqbwgduewevbcdkshjclweujdbfckdsw

When I test the script I gave you, I’m not receiving the same error so make sure you haven’t accidentally changed a value somewhere

How does your script look like now?

it still says that
Screenshot 2024-01-11 172012

I need to see the whole script, please

here

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local dataStore = DataStoreService:GetDataStore("PlayerData") -- You can rename this if you want

local canSave = {}

Players.PlayerAdded:Connect(function(player)
	local success, values = pcall(dataStore.GetAsync, dataStore, player.UserId)

	if success then
		local Coins = Instance.new("IntValue")
		Coins.Name = "Coins"
		Coins.Value = values and values.Coins
		Coins.Parent = player

		local Gems = Instance.new("IntValue")
		Gems.Name = "Gems"
		Gems.Value = values and values.Gems
		Gems.Parent = player

		canSave[player] = true
	else
		warn(values)
	end
end)

local function saveData(player)
	if canSave[player] then
		local success, result = pcall(dataStore.SetAsync, dataStore, player.UserId, {
			Coins = player.Coins.Value,
			Gems = player.Gems.Value
		})

		canSave[player] = nil

		if not success then warn(result) end
	end
end

Players.PlayerRemoving:Connect(saveData)

if RunService:IsStudio() then
	game:BindToClose(function()
		task.wait(4)
	end)
else
	game:BindToClose(function()
		local x, y = 0, 0

		for _, player in Players:GetPlayers() do
			x += 1

			coroutine.wrap(function()
				saveData(player)

				y += 1
			end)()
		end

		repeat task.wait() until y == x
	end)
end		
coroutine.wrap(function(player)
	saveData(player)

	y += 1
end)(player)

repeat task.wait() until y == x
end)
end

this is the part that says error

do i put the new code to the wrong place?

When I copy and paste and test the script myself, it works without error

I’m noticing that the formatting is different so did you modify the script?

i dont think so, let me test it again

wait, what type of script does this script is for? is it serverscript?

It’s a server Script inside of ServerScriptService

maybe my pleacement is the one thats causing an error?
Screenshot 2024-01-11 172728

When I place mine in a Folder it’s working as well so the issue’s quite strange

Edit: @Xxmaster_farterxX I found what the problem is:

You I accidentally wrote the coroutine twice so remove the second one and make sure the code is exactly as follows:

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local dataStore = DataStoreService:GetDataStore("PlayerData") -- You can rename this if you want

local canSave = {}

Players.PlayerAdded:Connect(function(player)
	local success, values = pcall(dataStore.GetAsync, dataStore, player.UserId)

	if success then
		local Coins = Instance.new("IntValue")
		Coins.Name = "Coins"
		Coins.Value = values and values.Coins
		Coins.Parent = player

		local Gems = Instance.new("IntValue")
		Gems.Name = "Gems"
		Gems.Value = values and values.Gems
		Gems.Parent = player

		canSave[player] = true
	else
		warn(values)
	end
end)

local function saveData(player)
	if canSave[player] then
		local success, result = pcall(dataStore.SetAsync, dataStore, player.UserId, {
			Coins = player.Coins.Value,
			Gems = player.Gems.Value
		})

		canSave[player] = nil

		if not success then warn(result) end
	end
end

Players.PlayerRemoving:Connect(saveData)

if RunService:IsStudio() then
	game:BindToClose(function()
		task.wait(4)
	end)
else
	game:BindToClose(function()
		local x, y = 0, 0

		for _, player in Players:GetPlayers() do
			x += 1

			coroutine.wrap(function()
				saveData(player)

				y += 1
			end)()
		end

		repeat task.wait() until y == x
	end)
end
1 Like

its working!!! thanks a bunch!

1 Like

It was my fault actually, so sorry about that

wait, how do i change the value using a code? i tested using my existing localscript and it doesent save.

It will depend on when you’d like to change it during your game, but you’ll need to remember to change the value on the server not in LocalScripts else the server won’t see the new value

You’ll need to use RemoteEvents to tell the server to change the value

do when a remote event fire, the server script doesent change all players value?

You can make it change the value for every player in the game if you want by doing this:

remoteEvent.OnServerEvent:Connect(function()
	for _, player in game.Players:GetPlayers() do
		if player:FindFirstChild("Coins") then
			player.Coins += 1
			player.Gems += 1
		end
	end
end)

no, i want it to only change to the player that fired the event