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)
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)
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)
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)
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)