Infinite yield possible on WaitForChild

local Service = game:GetService("UserInputService")
local event = game.ReplicatedStorage:WaitForChild("CameraEquip")
local Equipped = false

Service.InputBegan:Connect(function(KeyboardInput)
	if KeyboardInput.KeyCode.Name == "G" then
         if Equipped == false then
		    event:FireServer(true)
            Equipped = true
         else 
            event:FireServer(false)
            Equipped = false
         end
	end
end)

Edit:
and now that I think of it, you don’t need this:

	humanoid.Died:Connect(function()
		humanoid:UnequipTools()
	end)

since the tool is automatically unequipped when the character is reset.

There is still the problem of the tool disappearing, unfortunately. I don’t know why it’s still happening because I am not changing the parent of the tool like before.

It means that it is waiting forever for the tool do this:

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 and tool then
		humanoid:EquipTool(tool)
	elseif not equip and not tool then
		humanoid:UnequipTools()
	end
	humanoid.Died:Connect(function()
		humanoid:UnequipTools()
	end)
end)

I can’t rightfully say why that is happening because I’m busy testing both scripts and they work as intended.

Do you have any other scripts running that could be changing it?

It might be a problem with another script because when the tool disappears, I’m pretty sure it is being destroyed and not returned to the player’s backpack.

For example, in the tool I also have a weld and animation script.

I don’t think those scripts are the culprit/s, firstly test to see if the tool is actually destroyed, if it is check if you have any other scripts that make changes to the tool.

Yeah it is definitely destroyed for some reason. I will check.

Just posting the two scripts together, I’ve also added a statement to the local script to make the backpack invisible so a player can’t use both ‘1’ and ‘G’ to equip/unequip the tool.

Script in ServerScriptService:

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
end)

Local script in StarterPlayerScripts:

local Service = game:GetService("UserInputService")
local event = game.ReplicatedStorage:WaitForChild("CameraEquip")
local Equipped = false
local CoreGui = game:GetService("StarterGui")
CoreGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)

Service.InputBegan:Connect(function(KeyboardInput)
	if KeyboardInput.KeyCode.Name == "G" then
         if Equipped == false then
		    event:FireServer(true)
            Equipped = true
         else 
            event:FireServer(false)
            Equipped = false
         end
	end
end)
1 Like

Thanks for that. I put it as the solution so people looking at this post don’t have to scroll through all the comments. I am going to put the scripts and tool in a baseplate alone and see if that works. That way I will know if it is another script causing the disappearing problem.

Okay cool, if you have any other problems you can just message me or reply on this thread.