Remote Event only connects to one player

  1. What do you want to achieve?
    I have a morph that changes the player into a specific character and clones some ability scripts for them. I have regular scripts with the functions and a localscript used for contextaction.
  2. What is the issue?
    I tested this out using two players and whenever a new player uses the morph, the previous player loses connection to the remote events.

Attack Script example:

local AttackEvent = Instance.new("RemoteEvent")
AttackEvent.Name = "AttackEvent"
AttackEvent.Parent = game.ReplicatedStorage

local Tool = script.Parent:WaitForChild("Weapon")
local Animator = script.Parent.Humanoid.Animator

-----------------------------------------------------------------------

local LeftFist = script.Parent.Weapon.LeftFist
local RightFist = script.Parent.Weapon.RightFist
local User = script.Parent
local Attacking = false

local WeaponSound = Instance.new("Sound")
WeaponSound.SoundId = "rbxassetid://5835032207"
WeaponSound.Volume = .12

local Combo1Animation = script.Parent.Humanoid.Combo1
local Combo2Animation = script.Parent.Humanoid.Combo2
local Combo3Animation = script.Parent.Humanoid.Combo3

local Combo1Track = Animator:LoadAnimation(Combo1Animation)
local Combo2Track = Animator:LoadAnimation(Combo2Animation)
local Combo3Track = Animator:LoadAnimation(Combo3Animation)

local Combo = 1
local LastAttack = 0
_G.Cooldown = .6

------------------------------------------------------------------------------------

local function Attacking()
	local Tick = tick()
	print("Attack")
	if Tick - LastAttack > _G.Cooldown then

		if Tool.Activated and Combo == 1 then
			Attacking = true
			Combo1Track:Play()
			WeaponSound.Parent = RightFist
			WeaponSound:Play()
			Combo = 2
			LastAttack = tick()
			task.wait(_G.Cooldown)
			Attacking = false
		else

			if Tool.Activated and Combo == 2 then
				Attacking = true
				Combo2Track:Play()
				WeaponSound.Parent = LeftFist
				WeaponSound:Play()
				Combo = 3
				LastAttack = tick()
				task.wait(_G.Cooldown)
				Attacking = false

			else

				if Tool.Activated and Combo == 3 then
					Attacking = true
					Combo3Track:Play()
					WeaponSound.Parent = RightFist
					WeaponSound:Play()
					Combo = 1
					LastAttack = tick()
					task.wait(_G.Cooldown)
					Attacking = false
				end	
			end
		end
	end
end

RightFist.Touched:connect(function(hit)
	local model = hit:FindFirstAncestorOfClass("Model")
	if model and model:FindFirstChild("Humanoid") and Attacking then
		model:FindFirstChild("Humanoid"):TakeDamage(10)
		Attacking = false
	end
end)

LeftFist.Touched:connect(function(hit)
	local model = hit:FindFirstAncestorOfClass("Model")
	if model and model:FindFirstChild("Humanoid") and Attacking == true then
		model:FindFirstChild("Humanoid"):TakeDamage(10)
		Attacking = false
	end
end)

AttackEvent.OnServerEvent:Connect(Attacking)

The script used for morphs destroys the previous instances of remote events, so there’s never more than one.

The localscript:

Tool.Activated:Connect(function(Attacking)
	AttackEvent:FireServer(Attacking)
end)

I’m fairly new and amateurish with roblox scripting and I can’t seem to understand the issue, any help would be greatly appreciated, thank you.

-- LocalScript
local Tool = script.Parent:WaitForChild("Weapon")
local AttackEvent = game.ReplicatedStorage:WaitForChild("AttackEvent")

Tool.Activated:Connect(function()
	AttackEvent:FireServer(true)  -- Send a boolean value indicating the tool is activated
end)
-- Server Script
local AttackEvent = game.ReplicatedStorage:WaitForChild("AttackEvent")

local function Attacking(isAttacking)  -- Rename the function argument
	if isAttacking then
		-- Your attacking logic here
		-- ...
	end
end

AttackEvent.OnServerEvent:Connect(Attacking)

No errors, but the issue still persists. Tool activated does still work for both players, but the AttackEvent just doesn’t seem to run for the first player after the second player uses it?

Change

local function Attacking(isAttacking)  -- Rename the function argument

To:

local function Attacking(player, isAttacking)  -- Rename the function argument

Since the first argument for OnServerEvent is always the Player that fired the event.

1 Like

Still the same problem, first player uses morph and AttackEvent works. Second player uses morph and AttackEvent stops running for the first player.

1 Like

i think it’s because the debounce is for both players

1 Like

I was looking at the script and I have some things to say:

  1. Dont use global _G, either use a module script or dont use it at all if avoidable
  2. Every variable that should be unique to the players currently isn’t, since both players are using the same variables. Instead you should make a table that stores the variables with a key of the player’s UserId

I can try prototyping it if you need help

Almost everything is for both players, which is very bad.

Sorry if the answer’s obvious, but why is both players using the same variable bad? I’m trying to make character-based fighting game and wanted to maintain these variables specific to the character.

Solved! I played around with the issue in studio for a while and eventually figured out that this can be fixed by changing the location of the created Event to be within the Tool. Many thanks to everyone that helped me.

1 Like

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