Tool not activating when cloned

(sorry if I posted in the wrong category)


So I’m writing a script that allows you to switch weapons when you press a number key (like modern fps games) and whenever I clone the tool to the player and equip it, it never activates. Anyone know why? The tool I’m cloning is the “Paintball Gun” by Roblox.


My script
local uis = game:GetService("UserInputService")
local rs = game:GetService("ReplicatedStorage")
local event = rs.EquipPrimary
local event2 = rs.EquipSecondary
local player = game:GetService("Players").LocalPlayer
local equippedprimary = false
local equippedsecondary = false
local guns = rs.Guns

game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)

uis.InputBegan:Connect(function(input, processed)
	if processed then return end
	if input.KeyCode == Enum.KeyCode.One and equippedprimary == false then
		local thing = player.Backpack:FindFirstChild("Pistol")
		if thing then
			local primary = player.Character.Primary.Value
			local gun = guns:FindFirstChild(primary)
			print(primary)
			if gun then
				local clone = gun:Clone()
				print("equipping gun")
				equippedprimary = true
				equippedsecondary = false
				clone.Parent = player.Character
				player.Character:WaitForChild("Humanoid"):EquipTool(clone)
			end
		elseif not thing then
			local primary = player.Character.Primary.Value
			local gun = guns:FindFirstChild(primary)
			print(primary)
			if gun then
				local clone = gun:Clone()
				print("equipping gun")
				equippedprimary = true
				equippedsecondary = false
				clone.Parent = player.Backpack
				player.Character:WaitForChild("Humanoid"):EquipTool(clone)
			end
		end
	end
end)

-- havent done the secondary gun code yet :P
2 Likes

So it looks like you are cloning the tool via a local script, which dont worry is a common mistake.

See sense the paintball gun uses a script (server script) if I recall correctly, cloning a script (server script) with a local script doesnt work, as server scripts run via the server, and cloning it via the client will just break it.

What you’d need to do is send a remote event to the server, then make the server clone the weapon and parent it to the players backpack

3 Likes

So I followed what you said but whenever I press One, the server script doesn’t run or print anything. If I need to send a video of what’s happening, I can.

Local script
local uis = game:GetService("UserInputService")
local rs = game:GetService("ReplicatedStorage")
local event = rs.EquipPrimary
local event2 = rs.EquipSecondary
local player = game:GetService("Players").LocalPlayer
local equippedprimary = player.Character:WaitForChild("EquippedPrimary").Value
local equippedsecondary = player.Character:WaitForChild("EquippedSecondary").Value
local guns = rs.Guns
local primary = player.Character.Primary.Value
local secondary = player.Character.Secondary.Value

game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)

uis.InputBegan:Connect(function(input, processed)
	print("pressed happened") -- this prints
	if processed then return end
	if input.KeyCode == Enum.KeyCode.One and equippedprimary == false then
		print("fired") -- this prints
		event:FireServer(primary, player, equippedprimary, equippedsecondary)
	end
end)

-- havent done the secondary code yet :P
Server script
local rs = game:GetService("ReplicatedStorage")
local event1 = rs.EquipPrimary
local event2 = rs.EquipSecondary
local guns = rs.Guns

event1.OnServerEvent:Connect(function(primary, player, equippedprimary, equippedsecondary)
	print("yes i fired") -- doesn't print
	local thing = player.Backpack:FindFirstChild("Pistol")
	if thing then
		local gun = guns:FindFirstChild(primary)
		print(primary)
		if gun then
			local clone = gun:Clone()
			print("equipping gun")
			equippedprimary = true
			equippedsecondary = false
			clone.Parent = player.Character
			player.Character:WaitForChild("Humanoid"):EquipTool(clone)
		end
	elseif not thing then
		local gun = guns:FindFirstChild(primary)
		print(primary)
		if gun then
			local clone = gun:Clone()
			print("equipping gun")
			equippedprimary = true
			equippedsecondary = false
			clone.Parent = player.Backpack
			player.Character:WaitForChild("Humanoid"):EquipTool(clone)
		end
	end
end)
1 Like

You don’t need to fire the player to the server:

event:FireServer(primary, equippedprimary, equippedsecondary)

and your server should be like this:

event1.OnServerEvent:Connect(function(player, primary, equippedprimary, equippedsecondary)
1 Like

Thanks, but it didn’t fix my issue.

1 Like

Does your server script print anything?

1 Like

Try using WaitForChild when you try to get the events, they probably havent loaded in, thus causing it not to work

1 Like

event1.OnServerEvent:Connect(function(primary, player, equippedprimary, equippedsecondary)

This is wrong, on a onServerEvent function the first element is always the user who fired the event so your primary will never be the correct value, invert the player and primary variables and dont send the player in your local script in the FireServer.

1 Like

KingVamp already corrected this here btw:
So pretty sure they are aware now!

1 Like

Well in that case only thing I see is what you already said, events must not be found properly by one of the scripts.

2 Likes

@5smokin The server script doesn’t print anything.
@FroDev1002 I’ve used WaitForChild, and yet, nothing. Should I send a place file?

(sorry for the late reply, I got busy :P)

Sigh
I just realized the server script was in server storage, not replicated storage.

1 Like