Infinite yield possible on WaitForChild

I made a server script that when a player presses a button, an event is fired so a camera tool equips. It works for a while but then it stops working and gives this error:

Also, when this error comes up, the tool disappears from the player’s hands.

Here is the server script:

local Event = Instance.new("RemoteEvent")
Event.Name = "CameraEquip"
Event.Parent = game.ReplicatedStorage
Equipped = false

Event.OnServerEvent:Connect(function(Player)
	if Equipped == false then
		Player.Backpack:WaitForChild("Camera").Parent = Player.Character
		Equipped = true
	else
		Player.Character:WaitForChild("Camera").Parent = Player.Backpack
		Equipped = false
	end
	Player.Character.Humanoid.Died:Connect(function()
		Equipped = false
	end)
end)

I have concerns about this variable you have.

Equipped = false

In the event, your game has more than one player that variable will ruin your system

Unless… Only one person will be allowed a camera

1 Like

That is not an error but a warning that indicates that it could possibly wait an unreasonable amount of time before it finds the child, you could simply add a timeout argument:

Player.Character:WaitForChild("Camera", 50).Parent = Player.Backpack --times out after 50 seconds

And it disappears from the player’s hands because you are taking it out of the player’s character and putting it into the backpack.

Rather than taking the tool in and out of the character/backpack like this, you could simply use humanoid:EquipTool(tool) and humanoid:UnequipTools() to equip/unequip the camera.

local Event = Instance.new("RemoteEvent")
Event.Name = "CameraEquip"
Event.Parent = game.ReplicatedStorage

Event.OnServerEvent:Connect(function(Player, Equip)
    local humanoid = Player.Character:WaitForChild("Humanoid")
    local tool = Player.Backpack:WaitForChild("Camera")
	if Equip == true then
		humanoid:EquipTool(tool)
	else
		humanoid:UnequipTools()
	end
	humanoid.Died:Connect(function()
		humanoid:UnequipTools()
	end)
end)

Every player should have it, and that will be a problem now that I realise. What should I change it to instead?

possibly check if it is equipped if the player has a camera in their character

Yeah, that makes a lot more sense. I totally forgot about the :Unequip and :Equip. However, I entered the script you gave me and it doesn’t seem to be working at all now.

Remember to parse in true or false for Equip, so if the player pressed the button to equip the tool parse in true

What do you mean by this? I am still relatively new to scripting, sorry.

https://developer.roblox.com/en-us/api-reference/event/Tool/Equipped
https://developer.roblox.com/en-us/api-reference/event/Tool/Unequipped

When you use event:FireServer(), and you want it to equip the tool.

Use event:FireServer(true)

This sets the Equip parameter in the OnServerEvent to true.

you can just make a serverside check, if the camera is in the backpack, or in the character to prevent exploiters from breaking the server code

I have done this but the problem is still occurring.

event:FireServer(false)

did you try doing that

That would be the same as putting the tool away.

I found a mistake when trying to get it to unequip.
Here is the updated script.

local Event = Instance.new("RemoteEvent")
Event.Name = "CameraEquip"
Event.Parent = game.ReplicatedStorage

Event.OnServerEvent:Connect(function(Player, Equip)
    local humanoid = Player.Character:WaitForChild("Humanoid")
    local tool = Player.Backpack:FindFirstChild("Camera") or Player.Character:FindFirstChild("Camera")
	if Equip == true then
		humanoid:EquipTool(tool)
	else
		humanoid:UnequipTools()
	end
	humanoid.Died:Connect(function()
		humanoid:UnequipTools()
	end)
end)

I want the player to be able to toggle whether the tool is equipped or not. For example. they would press G to equip the tool and if they were to press it again, it would put it away.

if Equip == true then
		humanoid:EquipTool(tool)
	else
		humanoid:UnequipTools()
	end

This would error because Equip isn’t defined. Nvm Mybad.

Event.OnServerEvent:Connect(function(Player, Equip)
1 Like

I have tested the script and it works, what you need to do is add a local script into StarterPlayerScripts that detects if the local player has pressed ‘G’, and you can have a boolean variable Equipped. If Equipped is equal to false use event:FireServer(true) else event:FireServer(false).

I have a local script in StarterGui:

local Service = game:GetService("UserInputService")

Service.InputBegan:Connect(function(KeyboardInput)
	if KeyboardInput.KeyCode.Name == "G" then
		game.ReplicatedStorage:WaitForChild("CameraEquip"):FireServer(true)
	end
end)

Where and how would I add the boolean variable??