Why is my script breaking?

	local tool = game.ReplicatedStorage:FindFirstChild("Sugar Cookie"):Clone()
	tool.Parent = player.Backpack
	for _,two in pairs(tool:GetChildren()) do
		if two:IsA ("LocalScript") then
			two.Disabled = false	
			wait()		
		end
	end
end)

This specific line is breaking:
tool.Parent = player.Backpack

Let me know of any other information you need!

You did not define player, provide the full script.
If that’s all you have, you must define player and check if their backpack exists before parenting it.

2 Likes

That’s my whole script.
I’m kind of new to this, how would I go about defining the player?

player = game.Players.LocalPlayer
2 Likes

Is this a localscript or a serverscript?

If this is a localscript, you should never clone a tool, because then the serverscripts within the tool won’t run because the change did not replicate.

2 Likes

Then you need to define player, you can do that by using a PlayerAdded event.

1 Like

Is it when a player joins, you give the tool to them?

1 Like

LocalPlayer doesn’t work in server scripts.

1 Like

It’s a clickdetector, like for cafe stuff

local clickDetector = script.Parent.ClickDetector -- Location

clickDetector.MouseClick:Connect(function(player)
    local tool = game.ReplicatedStorage:WaitForChild('Sugar Cookie')
    local clone = tool:Clone()
    clone.Parent = player:WaitForChild('Backpack')
    
    for _,two in ipairs(tool:GetChildren()) do
		if two:IsA("LocalScript") then
			two.Disabled = false	
		end
	end

    -- Side note: I don't see a reason why you need to enable the script when it can be enabled already
end)

Then you need to set up a connection that will listen for when the clickdetector is clicked.
You can do this by doing:

local ClickDetector = your_path

ClickDetector.MouseClick:Connect(function(Player) -- Player will be the player instance.
   local Backpack = Player:FindFirstChild('Backpack')
      if Backpack then
      -- Give them the tool.
   end
end)

Use ipairs over pairs as it is designed for arrays, :GetChildren returns an array.
pairs is mainly used for dictionaries.

Also, that wait() isn’t even needed and may slow down the script.

I just copy and pasted their loop, didn’t look much into it. I’ll fix; thanks

1 Like

Yeah looks like it’s breaking on this line still for some reason.
clone.Parent = player:WaitForChild('Backpack')

Don’t use WaitForChild on the server, the backpack should already exist once the player is added to the server.

However, check it anyways using FindFirstChild if it hasn’t been added when they click it.
Otherwise it’ll throw an error, or yield forever (until it exists).

2 Likes

Are you sure? I just tested the script and it’s working fine for me. Does output throw any errors?

Edit: @DecodedString 's suggestion of FindFirstChild will work just fine, as well as just player.Backpack

 
22:07:05.853  Stack Begin  -  Studio
 22:07:05.854  Script 'Workspace.Lemon.Script', Line 8  -  Studio - Script:8
 22:07:05.854  Stack End  -  Studio

Lemon.Script is the script we are working with.

Using this exact script:

local ClickDetector = script.Parent.ClickDetector

ClickDetector.MouseClick:Connect(function(Player) -- Player will be the player instance.
	local Backpack = Player:FindFirstChild('Backpack')
	if Backpack then
		local tool = game.ReplicatedStorage:WaitForChild('Sugar Cookie')
		local clone = tool:Clone()
		clone.Parent = Player:FindFirstChild('Backpack')
	end
end)