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)
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.
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.
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()
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
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")
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)
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)