How to fix this event problem

Basically the after the first event was activated, the event in the deploy unit will always actiave even if the first event wasnt activated

ive seen some other problems like this but they dont have a solution for it

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ContextActionService = game:GetService("ContextActionService")
local UserInputService = game:GetService("UserInputService")
local Player = game:GetService("Players").LocalPlayer
local DeployEvent = ReplicatedStorage.DeployEvent
local UnitFrame = Player.PlayerGui:WaitForChild("UnitGui").UnitFrame
local Mouse = Player:GetMouse()

function DeployUnit(Unit)
	UserInputService.InputEnded:Connect(function(InputObject) 
		if InputObject == Enum.UserInputType.MouseButton1 or Enum.UserInputType.Touch then
			print(Unit)
			DeployEvent:FireServer(Unit.UnitName.Value, Mouse.Hit)
		end
	end)
end

for Quantity, Unit in ipairs({UnitFrame.Unit1, UnitFrame.Unit2, UnitFrame.Unit3}) do
	Unit.Activated:Connect(function()
		DeployUnit(Unit)
	end)
end

You need to disconnect InputEnded after the remote is fired:

function DeployUnit(Unit)
local connection = nil — set up connection
connection = UserInputService.InputEnded:Connect(function(InputObject) 
		if InputObject == Enum.UserInputType.MouseButton1 or Enum.UserInputType.Touch then
			print(Unit)

DeployEvent:FireServer(Unit.UnitName.Value, Mouse.Hit)

			Connection:Disconnect() — disconnects InputEnded, preventing it from running again
		end
	end)
end
1 Like

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