Tool not in backpack when saving data

I’m making a game called firework simulator and I just added data saving but it isn’t putting the tool in the player’s backpack. It worked before, and it doesn’t work ever since I added data saving. Also, when I put prints in the CharacterAdded event, it doesn’t show. It’s like it isn’t detecting the character added. Here’s my code

'local Players = game:GetService(“Players”)
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local ServerStorage = game:GetService(“ServerStorage”)
local DataStore = game:GetService(“DataStoreService”):GetDataStore(“frdy”)

Players.PlayerAdded:Connect(function(player)
local folder = Instance.new(“Folder”)
folder.Name = “Leaderstats”
folder.Parent = player

local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Parent = folder
cash.Value = 0

local equipped = Instance.new("StringValue")
equipped.Name = "Equipped"
equipped.Parent = folder
equipped.Value = "RedFirework"

local redFireworkBought = Instance.new("BoolValue")
redFireworkBought.Name = "RedFireworkBought"
redFireworkBought.Parent = folder
redFireworkBought.Value = true

local blueFireworkBought = Instance.new("BoolValue")
blueFireworkBought.Name = "BlueFireworkBought"
blueFireworkBought.Parent = folder
blueFireworkBought.Value = false

local cashData
local equippedData
local redFireworkBoughtData
local blueFireworkBoughtData
print("hi")
local success, errormessage = pcall(function()
	cashData = DataStore:GetAsync(player.UserId.."-cash") or 0
	equippedData = DataStore:GetAsync(player.UserId.."-equipped") or "RedFirework"
	redFireworkBoughtData = DataStore:GetAsync(player.UserId.."-redFireworkBought") or true
	blueFireworkBoughtData = DataStore:GetAsync(player.UserId.."blueFireworkBought") or false
end)
print("mid")
if success then
	cash.Value = cashData
	equipped.Value = equippedData
	redFireworkBought.Value = redFireworkBoughtData
	blueFireworkBought.Value = blueFireworkBoughtData
	print("loaded")
else
  	warn(errormessage)
end
print("past")
player.CharacterAdded:Connect(function(char)
	print("characteradded")
	local backpack = player:WaitForChild("Backpack")
	local tool
	if DataStore:GetAsync(player.UserId"-equipped") ~= nil then
		tool = game:GetService("ServerStorage").Tools[equippedData]:Clone()
	else
		tool = game:GetService("ServerStorage").Tools.RedFirework:Clone()
	end
	tool.Parent = backpack
end)

end)’

1 Like

Where is your script located?


Did you check the game settings to enable api calls?

I would make sure you did this first but I noticed you didn’t concat the uid and ‘-equipped’ so here’s the fixed version of that

local Players = game:GetService('Players')
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local ServerStorage = game:GetService('ServerStorage')
local DataStore = game:GetService('DataStoreService'):GetDataStore('frdy')

Players.PlayerAdded:Connect(function(player)
	local folder = Instance.new('Folder')
	folder.Name = 'Leaderstats'
	folder.Parent = player

	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Parent = folder
	cash.Value = 0

	local equipped = Instance.new("StringValue")
	equipped.Name = "Equipped"
	equipped.Parent = folder
	equipped.Value = "RedFirework"

	local redFireworkBought = Instance.new("BoolValue")
	redFireworkBought.Name = "RedFireworkBought"
	redFireworkBought.Parent = folder
	redFireworkBought.Value = true

	local blueFireworkBought = Instance.new("BoolValue")
	blueFireworkBought.Name = "BlueFireworkBought"
	blueFireworkBought.Parent = folder
	blueFireworkBought.Value = false

	local cashData
	local equippedData
	local redFireworkBoughtData
	local blueFireworkBoughtData
	print("hi")
	local success, errormessage = pcall(function()
		cashData = DataStore:GetAsync(player.UserId.."-cash") or 0
		equippedData = DataStore:GetAsync(player.UserId.."-equipped") or "RedFirework"
		redFireworkBoughtData = DataStore:GetAsync(player.UserId.."-redFireworkBought") or true
		blueFireworkBoughtData = DataStore:GetAsync(player.UserId.."blueFireworkBought") or false
	end)
	print("mid")
	if success then
		cash.Value = cashData
		equipped.Value = equippedData
		redFireworkBought.Value = redFireworkBoughtData
		blueFireworkBought.Value = blueFireworkBoughtData
		print("loaded")
	else
		warn(errormessage)
	end
	print("past")
	player.CharacterAdded:Connect(function(char)
		print("characteradded")
		local backpack = player:WaitForChild("Backpack")
		local tool
		if DataStore:GetAsync(player.UserId.."-equipped") ~= nil then
			tool = game:GetService("ServerStorage").Tools[equippedData]:Clone()
		else
			tool = game:GetService("ServerStorage").Tools.RedFirework:Clone()
		end
		tool.Parent = backpack
	end)
end)

Other than that though it worked for me

1 Like

it’s in the ServerScriptStorage

1 Like

Are you sure you don’t mean “ServerScriptService” and not “ServerScriptStorage”, as there does not exist any “ServerScriptStorage”?

1 Like

I will do that but the CharacterAdded isn’t getting detected because the print inside of it isn’t printing. The past is printing, but not the character added. Also I found another reason it’s not working it’s because I tried to put combine a tool and a string at the
‘tool = game:GetService(“ServerStorage”).Tools[equippedData]:Clone()’
because equippedData is a string and the Tools are a folder but I have no idea how to make it work.

1 Like

oops I meant serverscriptservice. Sorry haven’t been scripting for very long maybe only like 4 months

I enabled api calls. I need characters to post this.

The characteradded function doesn’t run because the previous code takes too long to run. You’re binding the function to run when the players character is created, so if that bind doesn’t happen before the character is first spawned, well then the code wont run. I’m sure if you respawn yourself ingame then it will run.

You also can’t combine a number and a string like this player.UserId"-equipped" instead use two dots to concat them.

so should I just remove the characterAdded?

You can move it to the top of the function to have the connect bind first.

but I want it to load saved data before I add it to the backpack because it won’t put the right rocket in if I haven’t loaded anything yet. Couldn’t I do
if not player.Character then
player.CharacterAdded:Connect(function()

 end)

end

That’s not an issue, the below code will still run before the character spawns. You can just access the equipped value directly where you parented it to the player. In the case that the datastore is actually mega slow and doesnt manage to run that brief code before spawn, just put a waitforchild inside of onCharacterAdded

Ok Thanks! I will see if it works later today. I will tell you if it works and mark yours as the solution

Hi, I did what you said and I’m getting the error “attempt to call number value”
here is my code
local Players = game:GetService(“Players”)
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local ServerStorage = game:GetService(“ServerStorage”)
local DataStore = game:GetService(“DataStoreService”):GetDataStore(“iuh”)

Players.PlayerAdded:Connect(function(player)
local folder = Instance.new(“Folder”)
folder.Name = “Leaderstats”
folder.Parent = player

local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Parent = folder
cash.Value = 0

local equipped = Instance.new("StringValue")
equipped.Name = "Equipped"
equipped.Parent = folder
equipped.Value = "RedFirework"

local redFireworkBought = Instance.new("BoolValue")
redFireworkBought.Name = "RedFireworkBought"
redFireworkBought.Parent = folder
redFireworkBought.Value = true

local blueFireworkBought = Instance.new("BoolValue")
blueFireworkBought.Name = "BlueFireworkBought"
blueFireworkBought.Parent = folder
blueFireworkBought.Value = false

local cashData
local equippedData
local redFireworkBoughtData
local blueFireworkBoughtData
player.CharacterAdded:Connect(function(char)
	print("characteradded")
	local backpack = player:WaitForChild("Backpack")
	local tool
	if DataStore:GetAsync(player.UserId"-equipped") ~= nil then
		tool = game:GetService("ServerStorage").Tools..equippedData:Clone()
	else
		tool = game:GetService("ServerStorage").Tools.RedFirework:Clone()
	end
	tool.Parent = backpack
end)
print("hi")
local success, errormessage = pcall(function()
	cashData = DataStore:GetAsync(player.UserId.."-cash") or 0
	equippedData = DataStore:GetAsync(player.UserId.."-equipped") or "RedFirework"
	redFireworkBoughtData = DataStore:GetAsync(player.UserId.."-redFireworkBought") or true
	blueFireworkBoughtData = DataStore:GetAsync(player.UserId.."blueFireworkBought") or false
end)
print("mid")
if success then
	cash.Value = cashData
	equipped.Value = equippedData
	redFireworkBought.Value = redFireworkBoughtData
	blueFireworkBought.Value = blueFireworkBoughtData
	print("loaded")
else
  	warn(errormessage)
end
print("past")

end)

Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
DataStore:SetAsync(player.UserId…“-cash”, player.Leaderstats.Cash.Value)
DataStore:SetAsync(player.UserId…“-equipped”, player.Leaderstats.Equipped.Value)
DataStore:SetAsync(player.UserId…“-redFireworkBought”, player.Leaderstats.RedFireworkBought.Value)
DataStore:SetAsync(player.UserId…“-blueFireworkBought”, player.Leaderstats.BlueFireworkBought.Value)
end)

if success then
	print("saved")
else
	warn(errormessage)
end

end)

ok I fixed that problem now I have a new one. I’m getting an error “attempted to call a missing method Clone of string”
the only thing I changed was this part of the code

player.CharacterAdded:Connect(function(char)
print(“characteradded”)
local backpack = player:WaitForChild(“Backpack”)
if DataStore:GetAsync(player.UserId…“-equipped”) ~= nil then
local tool = game:GetService(“ServerStorage”).Tools…equippedData:Clone()
tool.Parent = backpack
else
local tool = game:GetService(“ServerStorage”).Tools.RedFirework:Clone()
tool.Parent = backpack
end
end)

Ok I fixed it thank you so much it works! but now I’ve run into a different problem lol the blueFireworkBought isn’t saving.

could’ve marked it as solved, because I just tried to fix it:


function addTools(player, equippedData)
	local backpack = player:WaitForChild("Backpack")
	local tool
	if DataStore:GetAsync(player.UserId"-equipped") ~= nil then
		tool = game:GetService("ServerStorage").Tools[equippedData]:Clone()
	else
		tool = game:GetService("ServerStorage").Tools.RedFirework:Clone()
	end
	if tool then
		tool.Parent = backpack
	end
end
if player.Character then
		print("character already added")
		addTools(player, equippedData)
	end
	player.CharacterAdded:Connect(function(char)
		print("characteradded")
		addTools(player, equippedData)
	end)

Sorry I forgot. But are you able to fix my other problem? blueFireworkBought isn’t saving.