I’ve created a gun that sends a remote event to the server so that it can make bullets appear, the problem is that having more than one person use the gun causes both guns to conjure bullets pelletbug.wmv (2.4 MB)
Am I doing the right thing by using remote events to make bullets? or do I need to take another approach to part guns?
MY CODE:
local tool = script.Parent
game:GetService("ReplicatedStorage")
local fire = game.ReplicatedStorage:WaitForChild("fire")
local function shoot(player)
local bullet = Instance.new("Part", workspace)
bullet.Shape = 'Ball'
bullet.Material = 'Neon'
bullet.Color = Color3.new(1, 0, 0)
bullet.Size = Vector3.new(.5, .5, .5)
bullet.Position = tool.Handle.Position
wait(1)
bullet:Destroy()
end
fire.OnServerEvent:Connect(shoot)
MY WORKSPACE:
NINJA EDIT
I’m also including the code that fires the server event, just in case you need it
local player = game.Players.LocalPlayer
local tool = script.Parent
local mouse = player:GetMouse()
local rof = .1 --ROF for the gun
local activate = false
game:GetService("ReplicatedStorage")
local fire = game.ReplicatedStorage:WaitForChild("Fire")
print(fire)
mouse.Button1Up:connect(function()
activate = false
end)
mouse.Button1Down:connect(function()
activate = true
while activate
do
fire:FireServer()
print("E")
wait(rof)
end
end)
As seen in that video, there are two tools in the workspace. Let’s say you fire the event in the localscript in one tool. Both of the “Fire” scripts in the two tools see that the event is triggered, so they both run the code which conjures the bullets.
I believe you can fix this by checking if the localscript’s parent is the same as the other script’s parent. I haven’t tested this code yet, so see if it works:
“Fire”:
local tool = script.Parent
game:GetService("ReplicatedStorage")
local fire = game.ReplicatedStorage:WaitForChild("fire")
local function shoot(player,t)
if tool ~= t then return end --Checks if the tool value sent over is the same as the tool value here in the localscript, if not then it returns
local bullet = Instance.new("Part", workspace)
bullet.Shape = 'Ball'
bullet.Material = 'Neon'
bullet.Color = Color3.new(1, 0, 0)
bullet.Size = Vector3.new(.5, .5, .5)
bullet.Position = tool.Handle.Position
wait(1)
bullet:Destroy()
end
fire.OnServerEvent:Connect(shoot)
“Trigger”:
local player = game.Players.LocalPlayer
local tool = script.Parent
local mouse = player:GetMouse()
local rof = .1 --ROF for the gun
local activate = false
game:GetService("ReplicatedStorage")
local fire = game.ReplicatedStorage:WaitForChild("Fire")
print(fire)
mouse.Button1Up:connect(function()
activate = false
end)
mouse.Button1Down:connect(function()
activate = true
while activate
do
fire:FireServer(tool) --Sends the tool value over to the serverscript, ("Fire")
print("E")
wait(rof)
end
end)
I only changed two lines of code. Here’s what I did: I ran the tool variable from the localscript (“Trigger”) to the serverscript (“Fire”), and then in the serverscript I check if this tool variable that we got from the localscript is the same as the tool from the serverscript. If it is not the same, then it returns (breaks out of the function).
To answer your question, yes. You would need a RemoteEvent. The client has to be the one getting user input, while the server should be making the bullet if you want it to replicate.