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