Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to prevent remote events firing multiple times?

Once again, Remote Events have come to haunt me. The following is a local script from my egg launcher. Everything works fine except one issue. It launches two eggs at once which spawn on top of each other and get really wonky. I've confirmed that none of the scripts are running multiple times, the event just fires twice or more. I have no idea how I could control this in any way and from what I've seen on the forums, remote events are just horrendous to work with. Any help with why it's firing multiple times would be really appreciated

function fire()

	local vCharacter = Tool.Parent;
	local dir = HRT.CFrame.lookVector
	dir = computeDirection(dir)
	local pos = SpawnPart.Position + (SpawnPart.Position - HRT.Position)*2
	local EggY = HRT.Orientation.Y

	local missilePosition = pos
	local missileCFrame = CFrame.new(pos,  pos + dir)
	local missileOrientation = HRT.Orientation	

	if not debounce then
		debounce = true
		print("RanLocalFireFunction")
		fireEvent:FireServer(missileCFrame)
		debounce = false
	end
end
function computeDirection(vec)
	local lenSquared = vec.magnitude * vec.magnitude
	local invSqrt = 1 / math.sqrt(lenSquared)
	return Vector3.new(vec.x * invSqrt, vec.y * invSqrt, vec.z * invSqrt)
end

Tool.Enabled = true
function onActivated()
	wait(1)
	Tool.Enabled = false
	IsAttacking = true
	local character = Tool.Parent;
	local humanoid = character.Humanoid
	if humanoid == nil then
		print("Humanoid not found")
		return 
	end

	local targetPos = humanoid.TargetPoint
	fire()
	local yolkomaticTrack = YolkOMaticModel.AnimationController:LoadAnimation(yolkomaticani)
	yolkomaticTrack:Play()
	yolkomaticTrack.Looped = false
	wait(0.15)
	Tool.Enabled = true
end

Here is the script that responds to the firing.

Fired = game.ReplicatedStorage.YolkOMaticFireEvent.OnServerEvent:Connect(function(player, missileCFrame)
	Fired:Disconnect()
	--Launcher code that has no effect on the Remote Event issue
	--
	--
	--
end)

1 comment

1

hmm, try adding a wait in that debounce if statment? since :FireServer is the one that activates first thats probarly where the problem is from

1 answer

✓ Accepted answer

As starmaq stated, add a wait(1) between line 16 and 17

2 comments

0

Thanks for the response, however, I just realized my issue and it was a facepalm kind. I had the remote event in replicated storage, all I had to do was move it to the tool and now it only fires once. I'm not sure why this happens but I realize that thats how most Roblox tools work now.

0

you lucky man, if i only answerd without using comments xd

This discussion is preserved as it was. New answers, comments, and votes are closed.