Savedata() example

Can anyone please give me exa\mple to how to use savedata much appreciated

there is no function named savedata(), but im guessing you want to make one.

you’ll need to get datastoreservice and the datastore you’ll save

local ds = game:GetService("DataStoreService")
local coinsData = ds:GetDataStore("Coins")

then make leaderstats or folder that contains data you would save

-- Variables

local ds = game:GetService("DataStoreService")
local coinsData = ds:GetDataStore("Coins")

-- Logic

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

	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = leaderstats
end)

now lets make the data save function ( finally lol )

-- Variables

local ds = game:GetService("DataStoreService")
local coinsData = ds:GetDataStore("Coins")

-- Logic

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

	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = leaderstats
end)

function savedata(plr)
	coinsData:SetAsync(plr.UserId.."_coins", plr.leaderstats.Coins.Value)
end

also you could connect the function to PlayerRemoving so it saves once the player leaves

-- Variables

local ds = game:GetService("DataStoreService")
local coinsData = ds:GetDataStore("Coins")
local Players = game:GetService("Players")

-- Logic

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

	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = leaderstats
end)

-- Save function
function savedata(plr)
	coinsData:SetAsync(plr.UserId.."_coins", plr.leaderstats.Coins.Value)
end

-- Connecting to left event
Players.PlayerRemoving:Connect(savedata)

remember though you can always read this if you dont understand

2 Likes