Bow System Firing for all people who Have the bow equipped! (Using RemoteEvents)

Recently I have been making a Bow model / tool for my game. Everytime I click it fires through a RemoteEvent and it continues from there. The LocalScript and ServerScript are located inside the bow and the Event is in a folder in ReplicatedStorage.

But when I shoot it fires for all players and it is really weird. I know this will do for all players. And all dummies weilding the bow as well. How would I go along to fix this?

ServScript:

--// Services
local ReplicatedStorage = game:GetService("ReplicatedStorage");
local TweenService = game:GetService("TweenService");
local Debris = game:GetService("Debris");
local Players = game:GetService("Players")

--// Models
local Tool = script.Parent;
local Storage = Tool:FindFirstChild("Storage");
local Handle = Tool:FindFirstChild("Handle");
local Character = nil;
local Humanoid = nil

--// Values
local CanShoot = true;

--// Animations
local StillAnim = nil;
local AttackAnim = nil

--// Events
local FireEvent = ReplicatedStorage:FindFirstChild("Events"):FindFirstChild("Fire")

--[[]]----[[]]----[[]]--

--// Functions, 1
local Arg1 = function(Variable)
	if Variable == "Equip" then
		--[print(Variable)
		Character = Tool.Parent;
		Humanoid = Character:FindFirstChild("Humanoid")
		StillAnim = Humanoid:LoadAnimation(Storage.IdleAnimation)
		AttackAnim = Humanoid:LoadAnimation(Storage.Attack)

		Humanoid.WalkSpeed = Storage.WalkSpeed.Value
		if StillAnim ~= nil then
			StillAnim:Play()
		end
	end

	--\\//--

	if Variable == "Dequip" then
		--[print(Variable)
		if Character ~= nil and StillAnim ~= nil then
			Humanoid.WalkSpeed = 16
			StillAnim:Stop()
		end
	end
end

--[[]]----[[]]----[[]]--

FireEvent.OnServerEvent:Connect(function(player, position)
	if CanShoot == true then
		CanShoot = false
		
		local Arrow = Instance.new("Part");
		Arrow.Parent = workspace
		Arrow.Name = "Arrow"
		Arrow.Size = Vector3.new(2, 2, 2)
		Arrow.Anchored = true
		Arrow.CanCollide = false
		Arrow.BrickColor = BrickColor.new("White")

		--[[local Mesh = Instance.new("SpecialMesh");
		Mesh.Parent = Arrow
		Mesh.MeshId = "rbxassetid://2592153631"
		Mesh.Scale = Vector3.new(.0125, .0125, .0075)--]]
		
		local params = RaycastParams.new()
		params.FilterType = Enum.RaycastFilterType.Blacklist
		params.FilterDescendantsInstances = { player.Character, workspace.BossAttackParts }
		
		local origin = Handle.Position
		local direction = (position - origin).Unit * 300
		--local result = workspace:Raycast(origin, direction)
		
		local result = workspace:Raycast(origin, direction, params)
		local intersection = result and result.Position or origin + direction
		local distance = (origin - intersection).Magnitude

		local bullet_clone = Arrow
		bullet_clone.Size = Vector3.new(0.1, 0.1, distance)
		bullet_clone.CFrame = CFrame.new(origin, intersection) * CFrame.new(0, 0, -distance / 2)
		bullet_clone.Parent = workspace
		
		Handle["Bow Fire"]:Play()
		AttackAnim:Play()

		if result then
			local part = result.Instance
			local humanoid = part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid")
			
			if humanoid then
				humanoid:TakeDamage(50000)
			end
		end

		task.wait(.1)
		bullet_clone:Destroy()
		
		task.wait(.3)
		CanShoot = true
	end
end)

--// Functions, 2
Tool.Equipped:Connect(function()
	Arg1("Equip");
end)

Tool.Unequipped:Connect(function()
	Arg1("Dequip");
end)

--[[local TweenArrow = TweenService:Create(Arrow, TweenInfo.new(.1, Enum.EasingStyle.Linear), {Position = CF.Position})
		TweenArrow:Play()

		Arrow.Touched:Connect(function(Hit)
			local Player = Players:GetPlayerFromCharacter(Hit.Parent);

			if not Player then
				local NPC = Hit.Parent;

				if NPC:FindFirstChild("Humanoid") and NPC.Torso.CollisionGroup ~= "Statue" then
					NPC.Humanoid:TakeDamage(math.random(2100, 3000))
					Debris:AddItem(Arrow, 0)
				end
			end
		end)

		Debris:AddItem(Arrow, .2)

		task.wait(.4)
		
		CanShoot = true--]]

LocScript:

--// Services
local ReplicatedStorage = game:GetService("ReplicatedStorage");
local Players = game:GetService("Players")

--// Models
local Tool = script.Parent;
local Player = Players.LocalPlayer;
local Mouse = Player:GetMouse()

--// Values
local Time = .4;

--// Events
local FireEvent = ReplicatedStorage:FindFirstChild("Events"):FindFirstChild("Fire")

--[[]]----[[]]----[[]]--

--// Functions, 1
local Arg1 = function(Variable)
	if Variable == "Click" then
		local CF = Mouse.Hit.Position
		FireEvent:FireServer(CF)
	end
end

--// Functions, 2
Tool.Activated:Connect(function()
	Arg1("Click");
end)

The reason it’s firing for every player is because every server script is connected to the same remote in replicatedStorage. You have 2 options to fix this:

  • Either add a remoteEvent to every tool and have each tool access it’s own respective event.

  • Or have 1 server script (rn you have 1 script for every tool) that listens for the event and fires the bow for that player (I recommend doing this as it’s better and easier to scale)

I am having a problem with the first solution where it keeps erroring

Players.AVCOIL5.Backpack.Bow.Client:20: attempt to index nil with 'WaitForChild'  -  Client - Client:20

Really weird and I don’t know why. it clearly exists. (I switched to BindableEvents)

image

Here is my updated CliScript:

--// Services
local ReplicatedStorage = game:GetService("ReplicatedStorage");
local Players = game:GetService("Players")

--// Models
local Tool = script.Parent;
local Player = Players.LocalPlayer;
local Mouse = Player:GetMouse();
local Storage = Tool:FindFirstChild("Storage")

--// Events
local FireEvent = nil

--[[]]----[[]]----[[]]--

--// Functions, 1
local Arg1 = function(Variable)
	if Variable == "Click" then
		local CF = Mouse.Hit.Position
		FireEvent = Storage:WaitForChild("Fire")
		if FireEvent then
			--
			FireEvent:Fire(CF)
			--
		end
	end
end

--// Functions, 2
Tool.Activated:Connect(function()
	Arg1("Click");
end)

The storage object might no exist when the code ran. Try using waitForchild:

local Storage = Tool:WaitForChild("Storage")

I tried this as well. It still doesn’t work. In the script I am also changing the value after so it definitely shouldn’t be nil.

Why not just define the event at the start instead of redefining it every time the player clicks.

local Storage = Tool:WaitForChild("Storage")

--// Events
local FireEvent = Storage.Fire

Also just saw your comment of switching to bindable events. This wouldn’t work as bindable events can only communicate in their respective environment. This means if a client script fires a bindable event only another client script can listen for it. Same with server scripts. Bindable events are used to communicate server to server or client to client. You will NEED to use remoteEvents to send data to the server. There’s no work around.

I am still getting the same result. I am also sorry if I am being a little hard to work with here.

No worries. Could you go into a play test via studio → in explorer go to your player → backpack and then check if the remote exist in the bow (screenshot what you see). Also make sure you don’t have any objects that share the same name (e.g if u had 2 objects named storage and we’re referencing the wrong one).

And lastly just open the output and screenshot any errors or warnings that pop up


Oh, I know how to fix the “Fire” problem. Just something with the BindableEvents.

This error is from your server script. Could u post it here

Wait… I THINK i just got it working… Theres no way I just had to add a little “task.wait(1)” at the start of both codes. I’ll update you.

Sure!!!

Forgot my obs settings. Heres a ss

Oh I meant like the whole script or the line where the error occured. Pretty sure it’s cause you tried to do “Fire.Event” when u were using bindable events

Also the video you sent above looks like it’s working?

Yes, the bow is working but i’m still getting an error. Not sure if this is much of a problem. I will try putting it as “nil” first and see if that resolves it. (Must not be a problem since I set the value to the Event later. I’ll update you)

I think I got it working, thank you for the help! Don’t think I would have gotten this.

No worries. Just glad everything’s working. If you encounter any other issues feel free to ask! Goodluck with you game!

Thanks!!!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.