Datastore not working

Hello,
i am making a datastore for an intvalue to save a name of a song the problem here is that i can’t get it fixed when i play it say’s

Unable to cast string to int64

i don’t know how to fix this this is the datastore script

local DS = game:GetService("DataStoreService"):GetDataStore("Save1")

local function OnCharacterAdded(char)
	game:GetService("RunService").Stepped:Wait()
	local plr = game.Players:GetPlayerFromCharacter(char)
end

function OnPlayerAdded(plr)

	plr.CharacterAdded:Connect(OnCharacterAdded)

	local stats = Instance.new("Folder")
	stats.Name = "Save1"
	stats.Parent = plr

	local stage = Instance.new("IntValue")
	stage.Name = "Save1-Name"
	stage.Parent = stats

	local TeleStage = Instance.new("NumberValue")
	TeleStage.Name = "Save1-SongId"
	TeleStage.Parent = stage.Parent

	local key = "id_" .. plr.userId

	local data = DS:GetAsync(key)

	if data then
		stage.Value = data
		TeleStage.Value = stage.Value
	else
		DS:GetAsync(key, stage)
	end

end

game.Players.PlayerAdded:Connect(OnPlayerAdded)

function OnPlayerRemoved(plr)
	local key = "id_" .. plr.userId
	local data = plr.Save1["Save1-Name"].Value
	DS:SetAsync(key, data)
end

game.Players.PlayerRemoving:Connect(OnPlayerRemoved)

Which specific line is it giving the error on?

There isn’t a line that it says it’s jus the error

Unable to cast string to int64

Yes, it says that, but it should say stack begin, then the line number and then the error right? or is that not the case here?

OnPlayerRemoved is not the right function if you are the last player in the game and you left the game close instantly so the script do not fire

Then how can i fix this cause i am already searching an hour lol

if you want to help me your great

try to fix it with this

game:BindToClose(function()
	local players = Players:GetPlayers()
	for _, player in pairs(players) do
	SaveData(player) -- Your Safe function
	end
end)

And put also your save funtion in

game.Players.PlayerRemoving:Connect(function(player)
	SaveData(player)
end)
1 Like

where do i put it i can’t get it srr

you have to put your “save script” in a function like this and put here all that thinks what you want to save

function SaveData(player)
	print("saving Data...")

	--Saving Money
	local cash = player:FindFirstChild("leaderstats"):FindFirstChild("Cash")
	CASH:SetAsync(player.UserId,cash.Value)

end

then you fire this function in

game:BindToClose(function()
	local players = Players:GetPlayers()
	for _, player in pairs(players) do
	SaveData(player) -- Your Safe function
	end
end)

and

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

its a simple example you have to do this with your code

Do i need to do it like this

local DS = game:GetService("DataStoreService"):GetDataStore("Save1")

game.Players.PlayerAdded:Connect(function(plr)
	local stats = Instance.new("Folder")
	stats.Name = "Save1"
	stats.Parent = plr

	local stage = Instance.new("IntValue")
	stage.Name = "Save1-Name"
	stage.Parent = stats

	local TeleStage = Instance.new("NumberValue")
	TeleStage.Name = "Save1-SongId"
	TeleStage.Parent = stage.Parent

	local key = "id_" .. plr.userId

	local data = DS:GetAsync(key)

	if data then
		stage.Value = data
		TeleStage.Value = stage.Value
	else
		DS:GetAsync(key, stage)
	end
end)

function SaveData(player)
	print("saving Data...")

	--Saving Money
	local Name = player.Save1["Save1-Name"]
	DS:SetAsync(player.UserId, Name.Value)

end

game.Players.PlayerRemoving:Connect(function(plr)
	local key = "id_" .. plr.userId
	local data = plr.Save1["Save1-Name"].Value
	DS:SetAsync(key, data)
end)

It’s saying the same thing

yes but change the last function to this

game.Players.PlayerRemoving:Connect(function(plr)
SaveData(plr)
end)
1 Like

So like this

local DS = game:GetService("DataStoreService"):GetDataStore("Save1")

game.Players.PlayerAdded:Connect(function(plr)
	local stats = Instance.new("Folder")
	stats.Name = "Save1"
	stats.Parent = plr

	local stage = Instance.new("IntValue")
	stage.Name = "Save1-Name"
	stage.Parent = stats

	local TeleStage = Instance.new("NumberValue")
	TeleStage.Name = "Save1-SongId"
	TeleStage.Parent = stage.Parent

	local key = "id_" .. plr.userId

	local data = DS:GetAsync(key)

	if data then
		stage.Value = data
		TeleStage.Value = stage.Value
	else
		DS:GetAsync(key, stage)
	end
end)

function SaveData(player)
	print("saving Data...")
	
	local SongId = player.Save1["Save1-SongId"]
	DS:SetAsync(player.UserId, SongId.Value)
	
	local Name = player.Save1["Save1-Name"]
	DS:SetAsync(player.UserId, Name.Value)

end

game.Players.PlayerRemoving:Connect(function(plr)
	SaveData(plr)
end)

It does it again…

add this one too

game:BindToClose(function()
	local players = Players:GetPlayers()
	for _, player in pairs(players) do
	SaveData(player) -- Your Safe function
	end
end)

Can’t you just do it this is taking a year but is it like this

local DS = game:GetService("DataStoreService"):GetDataStore("Save1")

game.Players.PlayerAdded:Connect(function(plr)
	local stats = Instance.new("Folder")
	stats.Name = "Save1"
	stats.Parent = plr

	local stage = Instance.new("IntValue")
	stage.Name = "Save1-Name"
	stage.Parent = stats

	local TeleStage = Instance.new("NumberValue")
	TeleStage.Name = "Save1-SongId"
	TeleStage.Parent = stage.Parent

	local key = "id_" .. plr.userId

	local data = DS:GetAsync(key)

	if data then
		stage.Value = data
		TeleStage.Value = stage.Value
	else
		DS:GetAsync(key, stage)
	end
end)

game:BindToClose(function(Players)
	local players = Players:GetPlayers()
	for _, player in pairs(players) do
		SaveData(player)
		-- Your Safe function
	end
end)

function SaveData(player)
	print("saving Data...")
	
	local SongId = player.Save1["Save1-SongId"]
	DS:SetAsync(player.UserId, SongId.Value)
	
	local Name = player.Save1["Save1-Name"]
	DS:SetAsync(player.UserId, Name.Value)

end

game.Players.PlayerRemoving:Connect(function(plr)
	SaveData(plr)
end)

Are you still alive can you maybe do it for me this is taking to long…

i think this is the problem try to remove spaces bettwen
like this local key = “id_”…plr.userId

1 Like

It is still not working this is so stupid cause there is no line where it errors

did you try to google this error? like in the roblox wiki?

1 Like