remoteEvent fires to all client

ok so i made a localscript which fires a remoteEvent when mouseButton1Click.

The problem is, when a single player fires the event, it applies the stuffs to every client when used onServerEvent, which i dont want.
i used onServerEvent because i want the stuffs to be visible to other players

localscript:

local rm = game.ReplicatedStorage.wormRemotes.addWorm

script.Parent.MouseButton1Click:Connect(function()
	rm:FireServer()
end)

server script: (script is very long, but basically it just makes adds a worm effect/parts that follows the player)

local RS = game:GetService("RunService")
local IKFramwork = {}
local segAmount = 50
local PhysicsService = game:GetService("PhysicsService")
local obstacles = "obstacles"
local pass = "pass"
PhysicsService:CreateCollisionGroup(obstacles)
PhysicsService:CreateCollisionGroup(pass)
PhysicsService:CollisionGroupSetCollidable(pass, obstacles, false)

local rm = game.ReplicatedStorage.wormRemotes.addWorm

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAppearanceLoaded:Connect(function(character)
		local db = false
		rm.OnServerEvent:Connect(function(player)
			if db == false then
				db = true
--makes folder
				local folder = Instance.new("Folder", workspace)
				folder.Name = player.Name.."'s Folder"
--makes character invisible
				for i,v in pairs(character:GetDescendants()) do
					if v:IsA("BasePart") then
						PhysicsService:SetPartCollisionGroup(v, pass)
						v.Transparency = 1
					else
						if v:IsA("Decal") then
							v.Transparency = 1
						end
					end
				end
--sets part that the worm will follow
				local start = Instance.new("Part", workspace)
				start.Name = player.Name.."'s followPart"
				start.Transparency = 1
				start.CanCollide = false
				start.Anchored = false
				start.Massless = true
				start.CFrame = character.HumanoidRootPart.CFrame
				PhysicsService:SetPartCollisionGroup(start, obstacles)
				local weld = Instance.new("Weld", start)
				weld.Part0 = start
				weld.Part1 = character.HumanoidRootPart
				weld.C0 = CFrame.new(0,2,0)
				local finish = start
				character.Head.Position = start.Position
--framework of the worm
				function IKFramwork.SolveIK(segments, startPart, endPart)
					local prevSegment = nil;
					for i = #segments, 1, -1 do
						local goalPosition;
						local currentSegment = segments[i];

						if (not prevSegment) then
							goalPosition = endPart.Position
						else
							goalPosition = (prevSegment.CFrame * CFrame.new(0,0,prevSegment.Size.Z/3)).p
						end

						local startPositionOfThisSegment = (currentSegment.CFrame * CFrame.new(0,0,currentSegment.Size.Z/3)).p

						currentSegment.CFrame = (CFrame.new(goalPosition, startPositionOfThisSegment) * CFrame.new(0,0,-currentSegment.Size.Z/3)) * CFrame.Angles(0,math.pi,0)

						prevSegment = currentSegment
					end
				end
--makes worm segments
				local function makeWormSegments(firstSegment, numberOfSegments)
					local maxArmReach = segAmount
					local segments = {}
					local sizeOfEachSegment = maxArmReach/numberOfSegments
					local frontVectorOfArm = firstSegment.CFrame.LookVector
					for i = 1, numberOfSegments do
						local segment = Instance.new("Part")
						segment.Shape = "Ball"
						segment.Material = "SmoothPlastic"
						segment.Anchored = true
						segment.CanCollide = true
						segment.Massless = true
						segment.Size = Vector3.new(2,2, sizeOfEachSegment)
						segment.BrickColor = BrickColor.new("Medium stone grey")
						segment.Name = "segment "..i
						segment.CFrame = firstSegment.CFrame * CFrame.new(0,0, -(firstSegment.Size.Z + sizeOfEachSegment * (i-1)))
						segment.Parent = folder
						PhysicsService:SetPartCollisionGroup(segment, obstacles)

						local attachment = Instance.new("Attachment", segment)
						attachment.Orientation = Vector3.new(90,0,0)
						local fire = Instance.new("Fire", attachment)
						fire.Color = Color3.new(163, 162, 165)
						table.insert(segments, segment)
					end
					local segSecond = folder:WaitForChild("segment 2")
					segSecond.Size = Vector3.new(1.5,1.5,1.5)
					local segFirst = folder:WaitForChild("segment 1")
					segFirst.Size = Vector3.new(1,1,1)

					local segBeforeLast = folder:WaitForChild("segment "..segAmount - 1)
					segBeforeLast.Size = Vector3.new(1.5,1.5,1.5)
					local segLast = folder:WaitForChild("segment "..segAmount)
					segLast.Size = Vector3.new(1,1,1)

					return segments
				end

				local segments = makeWormSegments(finish, segAmount)
--sets up the worm
				RS.Heartbeat:Connect(function()
					IKFramwork.SolveIK(segments, finish, start)
				end)
--destroys folders is character died
				character.Humanoid.Died:Connect(function()
					folder:Destroy()
					start:Destroy()
				end)
			end
		end)
	end)
end)

Your connection to rm.OnServerEvent is being made within the PlayerAdded event, so a new connection is made each time a player joins and is likely causing the problem of duplicate responses to the event. That connection should happen outside of the PlayerAdded event so that only one connection is made.

2 Likes