Modulescript remotefunction fires for all players rather than one

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

  1. What do you want to achieve? Fix the problem

  2. What is the issue? so i use a raycast hitbox for my fighting game, i use a raycast module that fires a remote function to indicate somethings been hit but when someone elses raycast hits something, another players connect function thing fires aswell

  3. What solutions have you tried so far? i have tried disconnecting the function after a set amount of time etc. 0.4 seconds and it hasnt worked

this is my code

local module2 = {}
params = OverlapParams.new()
local module = require(game.ReplicatedStorage.RayCast)
local module3 = require(game.ReplicatedStorage.Stun)
counter = script.count

function module2.attack()
	local cd = script.cd
	if cd.Value == false then
		cd.Value = true
		player = player
		if counter.Value == 1 then
			counter.Value = 2
			local anim1 = script.Parent.Parent.Humanoid:LoadAnimation(script.Punch1)
			anim1:Play()
		elseif counter.Value == 2 then
			counter.Value = 3
			local anim2 = script.Parent.Parent.Humanoid:LoadAnimation(script.Punch2)
			anim2:Play()
		elseif counter.Value == 3 then
			counter.Value = 1
			local anim3 = script.Parent.Parent.Humanoid:LoadAnimation(script.Punch3)
			anim3:Play()
		end
		script.Parent.Punching.Value = true
		task.spawn(function()
			local s,f = pcall(function()
				if not script.Parent.Parent then
					connect:Disconnect()
				end
			end)
			if f then
				connect:Disconnect()
			end
			
			for i = 1,2 do
				local rayDirection = script.Parent.Parent.HumanoidRootPart.CFrame.LookVector.Unit * 5
				local rayOrigin = script.Parent.Parent.Torso.Position
				local raycastParams = RaycastParams.new()
				raycastParams.FilterDescendantsInstances = {workspace.Map,script.Parent.Parent,workspace.Lobby,workspace.Spawns, workspace.lobbypart}
				raycastParams.FilterType = Enum.RaycastFilterType.Exclude
				raycastParams.IgnoreWater = false
				raycastParams.RespectCanCollide = true
				module:Raycast(rayOrigin,rayDirection,raycastParams)
				task.wait(0.1)
			end
		end)
		wait(0.4)
		script.Parent.Punching.Value = false
		task.wait(script.Parent.Parent.Speed.Value)
		cd.Value = false
	end
end

connect = module.RaySuccess:Connect(function(hit)
	if script.Parent.Punching.Value == true then
		if hit then
			local hum = hit.Parent:FindFirstChild("Humanoid")
			if hum then
				if hum.Health >= 1 then

					if game.Players:FindFirstChild(hit.Parent.Name) then

						if hit.Parent.Name ~= script.Parent.Name then
							if hit.Parent.IsRagdolled.Value == false then
								if hit.Parent.InArena.Value == true then
									if script.Parent.Parent.IsRagdolled.Value == false and hit.Name ~= "Hitbox" then
										local s,f = pcall(function()
											if hit.Parent:GetAttribute("Block") == true then
												script.Parent.Punching.Value = false
												local hiteffect = game.ReplicatedStorage.hit.Attachment:Clone()
												hiteffect.Parent = hit.Parent["Right Arm"]
												game.Debris:AddItem(hiteffect,3)
												hiteffect[" effect"]:Emit(15)
												hiteffect[" effect2"]:Emit(15)
												wait()
												hiteffect[" effect3"]:Emit(15)
												hiteffect["effect 4"]:Emit(15)
												hit.Parent.Blockbar.Value -= 35
												hit.Parent.LastHit.Value = script.Parent.Name
												return
											end
											if hit.Parent:GetAttribute("Counter") == true then
												hit.Parent.LastHit.Value = script.Parent.Name
												hit.Parent.IsRagdolled.Value = true
												return
											end
											local sound = script.punch:Clone()
											script.Parent.Punching.Value = false
											sound.Parent = hit
											sound:Play()
											local player = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
											local hiteffect = game.ReplicatedStorage.hit.Attachment:Clone()
											if script.count.Value == 2 then
												hiteffect.Parent = script.Parent.Parent["Right Arm"]
											elseif script.count.Value == 3 then
												hiteffect.Parent = script.Parent.Parent["Left Arm"]
											elseif script.count.Value == 1 then
												hiteffect.Parent = script.Parent.Parent["Right Arm"]
											end
											hiteffect[" effect"]:Emit(15)
											hiteffect[" effect2"]:Emit(15)
											wait()
											hiteffect[" effect3"]:Emit(15)
											hiteffect["effect 4"]:Emit(15)
											hit.Parent.IsRagdolled.Value = true
											--hit.Parent.RagdollTrigger.Value = true
											hum:TakeDamage(7)
											hit.Parent.LastHit.Value = script.Parent.Name
											if script.count.Value == 2 or script.count.Value == 3 then
												module3:Stun(hit.Parent,1)
											elseif script.count.Value == 1 then
												hit.Parent.RagdollTrigger.Value = true
												local velocity = Instance.new("BodyVelocity",hit.Parent.HumanoidRootPart)
												velocity.MaxForce = Vector3.new(3,3,3) * math.huge
												local dir = (hit.Parent.HumanoidRootPart.CFrame.Position - script.Parent.Parent.HumanoidRootPart.Position).Unit
												velocity.Velocity = (dir + Vector3.new(0,100,0)).Unit * script.Parent.Parent.Power.Value

												local rot = Instance.new("BodyAngularVelocity",hit.Parent.HumanoidRootPart)
												rot.AngularVelocity = Vector3.new(0.3,0.5,0.3) * math.pi *5
												rot.MaxTorque = Vector3.new(2,50,2) *math.huge
												rot.P = 10000
												game:GetService("Debris"):AddItem(velocity, 0.2)
												game:GetService("Debris"):AddItem(rot,0.2)
											end
											task.wait(0.3)
											hit.Parent.IsRagdolled.Value = false
											hit.Parent.RagdollTrigger.Value = false

											wait(0.3)
											sound:Destroy()
										end)
										if f then
											warn(f)
											script.Parent.Parent.IsRagdolled.Value = false
											script.Parent.Parent.RagdollTrigger.Value = false
											script.Parent.Parent.Humanoid.AutoRotate = true
											script.Parent.Parent.Humanoid.WalkSpeed = 16
										end
									end
								end
							end
						end
					end
				end
			end

		end
	end
end)

script.Parent.Parent.Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
	if script.Parent.Parent.Humanoid.Health <= 1 then
		connect:Disconnect()
	end
end)
return module2

any help would be appreciated

forgot to mention, this is my module script

local module = {}

local event = Instance.new("BindableEvent")
module.RaySuccess = event.Event

function module:Raycast(origin, direction, raycastParams)
	local ray = workspace:Raycast(origin,direction,raycastParams)
	if ray then
		if ray.Instance then
			event:Fire(ray.Instance, ray.Distance, ray.Position)
		end
	end
end


return module

i dont see a RemoteFunction or any invoke calls/connections anywhere

i meant to say bindable event my bad

oh ok, theres no default “player” value on bindableevents like there are on remotes (since they’re not needed). your best bet is either

  • having independent events for each player (so you always know a signal there belongs to a specific player),
TargetPlayer.BindableEvent:Fire() -- probably have a designated place and not just the player lol
  • or passing the desired player as a parameter, and checking to see if the executing player is the desired one. ex:
BindableEvent:Fire(TargetPlayer)

as an additional note, maybe consider lowering your scope by inverting the conditions and putting a return as their body
so instead of something like

connect = module.RaySuccess:Connect(function(hit)
	if script.Parent.Punching.Value == true then
		if hit then
			local hum = hit.Parent:FindFirstChild("Humanoid")
			if hum then
				-- rest of body
			end
		end
	end
end)

you could do something like

connect = module.RaySuccess:Connect(function(hit)
	if script.Parent.Punching.Value == false then -- changed to `false`. `~=` & `not` are also valid ways of inverting this 
		return
	end
	if not hit then -- added "not"
		return
	end
	local hum = hit.Parent:FindFirstChild("Humanoid")
	if not hum then return end -- added "not", and made it one line. Theres no performance or execution consequences of this, its just a design choice to reduce line count for some people (me). `== nil` is also valid
	-- rest of body
end)

I think I will make a separate event for each player as the function repeatedly firing for every player would be unoptimized, thank you for your help!

1 Like

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