How can i make a datastore for this script?

How can i make a data store for this script

local db = true
script.Parent.Touched:connect(function(hit)
if hit.Parent:FindFirstChild(“Humanoid”) ~= nil then
if db == true then
db = false
script.Parent.Transparency = 1
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 500
script.Sound:Play()
script.Parent.Transparency = 1
wait(1)
db = true
script.Parent.Transparency = 0
end
end
end)

5 Likes

I’m on mobile but I’ll try to help

local datastore = game:GetService(“DataStoreService”)
datastore:SetAsync(“User_”..player.UserId,player.leaderstats.Coins.Value)

I think this should work

1 Like

ok thanks ill try it now i love char limit

1 Like

You can Learn about Datastores here. You will want to load it on join, and save it when the player leaves, in a server script.

1 Like

this look right ?

1 Like

No, only save when OnPlayerRemoving is triggered

1 Like

where does it say that line# ?

1 Like

Also you can’t copy and paste " ", quoted things. You need to requote the words “DataStoreService” and “User_” (Why there is a red line under those words)

1 Like

In a new script in server script service do this:

local datastore = game:GetService(“DataStoreService”)
game.Players.OnPlayerRemoving:connect(function(Player)
datastore:SetAsync(“User_”..Player.UserId,Player.leaderstats.Coins.Value)
end)

I can’t really format this well because I’m on mobile :sweat_smile:

1 Like

You need to make a datastore lol with changing it to this

local datastore = game:GetService(“DataStoreService”):GetDataStore("Leaderstats")
1 Like

Btw it will be more complicated than just this if you want to save other stuff along with coins, and to make sure data loads, and to back up data etc. I’d recommend using datastore2 and learning how it works

1 Like

Oh I forgot about that oops sorry

1 Like

i already have a data store for tools here it is

local ToolFolder = game:GetService(“ServerStorage”):FindFirstChild(“SavedTools”)
local DataStoreService = game:GetService(“DataStoreService”)
local SaveData = DataStoreService:GetDataStore(“SaveData”)

game.Players.PlayerAdded:Connect(function(Player)
local ToolData = SaveData:GetAsync(Player.UserId)

local Backpack = Player:WaitForChild("Backpack")
local StarterGear = Player:WaitForChild("StarterGear")

if ToolData ~= nil then
	for i, v in pairs(ToolData) do
		if ToolFolder:FindFirstChild(v) and Backpack:FindFirstChild(v) == nil and StarterGear:FindFirstChild(v) == nil then
			ToolFolder[v]:Clone().Parent = Backpack
			ToolFolder[v]:Clone().Parent = StarterGear
		end
	end
end

Player.CharacterRemoving:Connect(function(Character)
	Character:WaitForChild("Humanoid"):UnequipTools()
end)

end)

game.Players.PlayerRemoving:Connect(function(Player)
local ToolTable = {}

for i, v in pairs(Player.Backpack:GetChildren()) do
	table.insert(ToolTable, v.Name)
end
if ToolTable ~= nil then
	SaveData:SetAsync(Player.UserId, ToolTable)
end

end)

1 Like
local ToolFolder = game:GetService(“ServerStorage”):FindFirstChild(“SavedTools”)
local DataStoreService = game:GetService(“DataStoreService”)
local SaveData = DataStoreService:GetDataStore(“SaveData”)

game.Players.PlayerAdded:Connect(function(Player)
local ToolData = SaveData:GetAsync(Player.UserId)

local Backpack = Player:WaitForChild("Backpack")
local StarterGear = Player:WaitForChild("StarterGear")

if ToolData ~= nil then
	for i, v in pairs(ToolData) do
		if ToolFolder:FindFirstChild(v) and Backpack:FindFirstChild(v) == nil and StarterGear:FindFirstChild(v) == nil then
			ToolFolder[v]:Clone().Parent = Backpack
			ToolFolder[v]:Clone().Parent = StarterGear
		end
	end
end

Player.CharacterRemoving:Connect(function(Character)
	Character:WaitForChild("Humanoid"):UnequipTools()
end)


end)

game.Players.PlayerRemoving:Connect(function(Player)
local ToolTable = {}


for i, v in pairs(Player.Backpack:GetChildren()) do
	table.insert(ToolTable, v.Name)
end
if ToolTable ~= nil then
	SaveData:SetAsync(Player.UserId, ToolTable)
end


end)

I’m formatting your code so I can read it easier

1 Like

Yea this isn’t gonna work if the player dies or anything like that in between joining and leaving… it would just revert data (ik it’s not your code btw)

no it works this isnt the script i am having problems with this one works perfectly

1 Like

im trying to make ALL leaderstats a data store

1 Like

Try joining, getting an item, then resetting

1 Like

i have another script that prevents that issue

1 Like

Why isn’t it just in the same script…?

1 Like