Basically my problem is the tools won’t clone to other players. But it only clones to the first person who joins, whoever joins after them it’ll error and not clone anything. Gladly you can still equip it from your inventory, but the script needs to loop check your owned folder and see if it exist and if you own it and then clone it into your backpack.
Here is the script that is having the problem. (PLEASE ASSIST ME RATHER THAN SIMPLY FIXING MY PROBLEM)
local DataStoreModule = require(ServerScriptService.DataStoreModule)
local template = {
leaderstats = {
FunkyBux = 0
},
Owned = {}
}
local function StateChanged(dataStore)
while not dataStore.State do
if dataStore:Open(template) ~= "Success" then
task.wait(6)
end
end
end
local function CheckTools(player)
for _, owned in pairs(player.Owned:GetChildren()) do
if owned:IsA("BoolValue") then
for _, tool in pairs(game.ServerStorage.Resources.Shop:GetDescendants()) do
if tool:IsA("Tool") and player.Owned:FindFirstChild(tool.Name) then
if not player.Backpack:FindFirstChild(tool.Name) and not player.Character:FindFirstChild(tool.Name) then
local clonedTool = tool:Clone()
clonedTool.Parent = player.Backpack
end
end
end
end
end
end
local function PlayerAdded(player)
local dataStore = DataStoreModule.new("funkybux002", player.UserId)
dataStore.StateChanged:Connect(function()
StateChanged(dataStore)
end)
StateChanged(dataStore)
player:WaitForChild("Owned")
CheckTools(player)
print(dataStore.Value.leaderstats.FunkyBux)
print(dataStore.Value)
print(dataStore.Value.Owned)
player.CharacterAdded:Connect(function()
CheckTools(player)
end)
end
for _, player in ipairs(game:GetService("Players"):GetPlayers()) do
task.spawn(PlayerAdded, player)
end
game.Players.PlayerAdded:Connect(PlayerAdded)
game.Players.PlayerRemoving:Connect(function(player)
local dataStore = DataStoreModule.new("funkybux002", player.UserId)
if dataStore then
dataStore:Destroy()
end
end)
What about the 6 second wait in the StateChanged function? That seems odd. I added a few print statement to check on that:
local DataStoreModule = require(ServerScriptService.DataStoreModule)
local template = {
leaderstats = {
FunkyBux = 0
},
Owned = {}
}
local function StateChanged(dataStore, player)
while not dataStore.State do
if dataStore:Open(template) ~= "Success" then
print("Not A Success, Waiting", player)
task.wait(6)
print("Waited.", player)
end
end
end
local function CheckTools(player)
for _, owned in pairs(player.Owned:GetChildren()) do
if owned:IsA("BoolValue") then
for _, tool in pairs(game.ServerStorage.Resources.Shop:GetDescendants()) do
if tool:IsA("Tool") and player.Owned:FindFirstChild(tool.Name) then
if not player.Backpack:FindFirstChild(tool.Name) and not player.Character:FindFirstChild(tool.Name) then
local clonedTool = tool:Clone()
clonedTool.Parent = player.Backpack
end
end
end
end
end
end
local function PlayerAdded(player)
local dataStore = DataStoreModule.new("funkybux002", player.UserId)
dataStore.StateChanged:Connect(function()
StateChanged(dataStore, player)
end)
StateChanged(dataStore, player)
player:WaitForChild("Owned")
CheckTools(player)
print(dataStore.Value.leaderstats.FunkyBux)
print(dataStore.Value)
print(dataStore.Value.Owned)
player.CharacterAdded:Connect(function()
CheckTools(player)
end)
end
for _, player in ipairs(game:GetService("Players"):GetPlayers()) do
task.spawn(PlayerAdded, player)
end
game.Players.PlayerAdded:Connect(PlayerAdded)
game.Players.PlayerRemoving:Connect(function(player)
local dataStore = DataStoreModule.new("funkybux002", player.UserId)
if dataStore then
dataStore:Destroy()
end
end)
I’m not sure what the error is since you haven’t sent it yet, but before that I noticed 2 things you can change in your script.
The CheckTools() function can be more efficient, aka you only need 1 loop instead of the 2 loops you have currently. You’ll want to remove one of the loops anduse :FindFirstChild() instead. Here it is.
You call CheckTools() inside the PlayerAdded function, this isn’t necessary and could be causing the error since the character hasn’t loaded in at this point.
Remove the waits inside the PlayerAdded() or put the CharacterAdded() function at the top since the :WaitForChild() functions stall and prevent the CharacterAdded() function from firing since the character has spawned before the function is connected.
local function CheckTools(player)
local character = player.Character
local backpack = player.Backpack
if not character then return end-- make sure the character exists (the player is loaded in)
for _, owned in pairs(player.Owned:GetChildren()) do
if owned:IsA("BoolValue") then
local tool = game.ServerStorage.Resources.Shop:FindFirstChild(owned.Name) -- Skip the 2nd loop, and simply use :FindFirstChild()
if tool and not backpack:FindFirstChild(tool.Name) and not character:FindFirstChild(tool.Name) then
local clonedTool = tool:Clone()
clonedTool.Parent = backpack
end
end
end
end
Try updating the CheckTools() code and remove calling it inside the PlayerAdded() function and keep it in the CharacterAdded() one.
local function CheckTools(player)
local character = player.Character
local backpack = player.Backpack
if not character then return end
print("Hello Test 1")
for _, owned in pairs(player.Owned:GetChildren()) do
print("Hello Test 2")
if owned:IsA("BoolValue") then
print("Hello Test 3")
local tool = game.ServerStorage.Resources.Shop:FindFirstChild(owned.Name)
if tool and not backpack:FindFirstChild(tool.Name) and not character:FindFirstChild(tool.Name) then
print("Hello Test 4")
local clonedTool = tool:Clone()
clonedTool.Parent = backpack
end
end
end
end
I added some prints, It doesn’t print Hello Test 4.