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:
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)
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.
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.
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).
local Service = game:GetService("UserInputService")
Service.InputBegan:Connect(function(KeyboardInput)
if KeyboardInput.KeyCode.Name == "G" then
game.ReplicatedStorage:WaitForChild("CameraEquip"):FireServer(true)
end
end)