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

help !! there something wrong with my script . The script won't save data when i left the game ?

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local cash = Instance.new("IntValue")
	cash.Name = "Kronos"
	cash.Parent = player
	
	local data
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsnyc(player.UserId.."-cash")
	end)
	
	if success then
		cash.Value = data
	else
		print("Failed to save data")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
    	myDataStore:GetAsnyc(player.UserId.."-cash",player.Cash.Vaule)
	end)
	
	if success then
		print("Data saved")
	else
		print("Failed to save data")
		warn(errormessage)
	end
end)

3 comments

0

Are you getting Error messages, or is it just not saving?

0

Im pretty sure that it just don't save

0

i get "Failed to save data"

1 answer

✓ Accepted answer

You cannot set it to GetAsync when player is removing, as it won't save data. Instead use SetAsync to save at when player is removing

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
    local cash = Instance.new("IntValue")
    cash.Name = "Kronos"
    cash.Parent = player

    local data
    local success, errormessage = pcall(function()
        data = myDataStore:GetAsnyc(player.UserId.."-cash")
    end)

    if success then
        cash.Value = data
    else
        print("Failed to save data")
        warn(errormessage)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local success, errormessage = pcall(function()
        myDataStore:SetAsync(player.UserId.."-cash", player.Cash.Vaule)
    end)

    if success then
        print("Data saved")
    else
        print("Failed to save data")
        warn(errormessage)
    end
end)

This should work

1 comment

0

thank

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