RemoteEvent fires depending on amount of players using tool

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    I would like to make it so that these scripts shoot one fireball regardless of the amount of players using the tool

  2. What is the issue?
    The script shoots multiple fireballs depending on the amount of players using the same tool, for example 4 players using the tool in the server launches 4 fireballs, and this causes FPS drops.

  3. What solutions have you tried so far?
    Did you look for solutions on the Developer Hub?
    Yes, I did and they helped me find an idea on the source of the problem, but those solutions were that there were multiple scripts which caused the problem. For me, there’s supposed to be multiple scripts as the other players’ tools wouldn’t operate without them.
    I tried adding a debounce timer too.
    I’m looking for a solution that doesn’t require me having to create a separate remoteevent for each player, as I’d have to edit it for every other ability tool, which would be a lengthly process, if there are any.

Script:


local db = false

game.ReplicatedStorage.PipeRemotes.FireballThrow.OnServerEvent:Connect(function(player, tool)
	if db == false then
		local playeR = game.Players:GetPlayerFromCharacter(tool.Parent)
		if player == playeR then
			local character = player.Character or player.CharacterAdded:Wait()
			local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
			if character:FindFirstChild("Flame") then
				db = true

				wait(.11)

--fireball logic works fine
				local function spawnFireball(positionOffset)

					local fireball = game.ReplicatedStorage.FX.Fireball:Clone()
					fireball.Parent = workspace.Visuals
					fireball:SetPrimaryPartCFrame(humanoidRootPart.CFrame * CFrame.new(positionOffset))

				local bv = Instance.new("BodyVelocity")
				bv.Parent = fireball
				bv.MaxForce = Vector3.one * math.huge
				bv.Velocity = humanoidRootPart.CFrame.LookVector * 50

					game.Debris:AddItem(fireball, 5)
				end

				spawnFireball(Vector3.new(0,0,-5))

				wait(18.9)
				db = false
			end
		end
	end
end)

LocalScript (in the Script):


local dbounce = false
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local cooldown = 20
local tool = script.Parent.Parent.Parent

local function fireball()
	if char:FindFirstChild("Flame") and dbounce == false and char:WaitForChild("RagdollTrigger").Value == false and hum.Health > 0 and not char:FindFirstChildOfClass("ForceField") then
		dbounce = true
		game.ReplicatedStorage.PipeRemotes.FireballThrow:FireServer(tool)
		wait(cooldown)
		dbounce = false
	end
end

local cas = game:GetService("ContextActionService")

tool.Equipped:Connect(function()
	cas:BindAction("Ability", fireball, true, Enum.KeyCode.E)
	cas:SetTitle("Ability", "Fireball")
	cas:SetPosition("Ability", UDim2.new(0.25, 0, 0.25, 0))
end)

tool.Unequipped:Connect(function()
	cas:UnbindAction("Ability")
end)

Tell me if you need any more information or clarification.
Thanks for any help you can provide! :happy1:

ehhh am i a bozo? am i missing context or something? i mean i dont get what youre trying to do exactly. what do you mean by shoot 1 fireball even though 4 players are using the tool? do you wanna wait till all players are ready and then shoot de fireball once they are? like, whats going on here? whats the tool?

I’m pretty sure he’s saying that if a player fires the remote event for the fireball, it for some reason fires X amount of times (X being the amount of players in the server)

hmm why in tarnation would that be the case?

This happens because you’ve put the server script inside the tool, Which I’m guessing gets duplicated each time someone receives the tool, Then each script creates a OnServerEvent connection with the remote causing it to fire depending on the amount of tools in the game currently.

Try moving the server script to somewhere where it won’t be duplicated, Or create a separate remote for each tool and assign it independantly.

My bad, I phrased the topic so badly, but my intention is to make it so that the ability tool shoots one fireball from the player when that player activates the ability, not anything confusing.

But I realised that the topics I looked at went completely over my head for some reason and I should have just put it in ServerScriptService to begin with.
Thanks.

P.S. Also I’m still new to DevForum posting so sorry about the horrendous format of this topic.

1 Like

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