Creating part for other players but not the player who fired RemoteEvent

-- Server 
local SpawnEvent = script.Parent:FindFirstChild("SpawnEvent")
local tool = script.Parent
local current_player = tool.Parent.Parent

SpawnEvent.OnServerEvent:Connect(function(player)
	for _, Player in pairs(game.Players:GetPlayers()) do
		if Player ~= current_player then
			SpawnEvent:FireClient(Player)
			print(Player.Name)
		else
			print("This is the current player.")
		end
	end
end)

-- Client
local tool = script.Parent
local player = tool.Parent.Parent
local SpawnEvent = tool:WaitForChild("SpawnEvent")

tool.Activated:Connect(function()
	SpawnEvent:FireServer()
	print("Client requests server to create part.")
end)


local function CreatePart()
	local part = Instance.new("Part")
	part.Name = "Creation"
	part.Parent = workspace
	part.CFrame = tool:FindFirstChild("Handle").CFrame
	print("Created part.")
end

SpawnEvent.OnClientEvent:Connect(CreatePart)

I’m currently trying to create a part for every player except the player who fired the RemoteEvent. So far it prints “Client requests server to create part” and prints the Player’s name who isn’t the current player but does not FireClient to create the part. What am I doing wrong?

SpawnEvent.OnServerEvent:Connect(function(player)
	for _, Player in pairs(game.Players:GetPlayers()) do
		if Player ~= player then -- Line that was changed.
			SpawnEvent:FireClient(Player)
			print(Player.Name)
		else
			print("This is the current player.")
		end
	end
end)

Try this an see if it works.

1 Like

I tried that but it still doesn’t create the part or print “Created part”. I’ve read that you’re able to create the part on the client and replicate it for all players but I’m not sure what I’m doing wrong?

Have you tried comparing the player’s name?
if Player.Name == player.Name

Edit: also current_player refers to the character not the player

You are trying to get the current_player which is the character instead you should say if Player ~= to player

Post the errors when you run the script.

That wouldn’t do any good in his case

The current_player prints out the player not the character. When I click on the output it directs me to Players.

1 Like

It doesn’t print out any errors.

Oh you’re right it saves the characters player

Edit: oof

Try putting the Remote Event in Replicated Storage, then make all references to the remote event equal to the one in the Replicated Storage.

I’m guessing what’s going on is you are parenting the Remote Event under the tool and that means you are creating a remote event for each tool instance clone, which means that Tool A’s remote event wouldn’t be fired by Tool B.

2 Likes

That actually fixed it. Thank you! I have another problem now, it clones the part twice for the other player how could I prevent it from cloning twice?

I’m going to guess you have the Server Script parented under the tool also. Just move that to ServerScriptService. In my opinion, you should only have LocalScripts parented to the tool in most cases.

EDIT: This also means you will have to change the server script to

SpawnEvent.OnServerEvent:Connect(function(player)
	for _, Player in pairs(game.Players:GetPlayers()) do
		if Player ~= player then
			SpawnEvent:FireClient(Player)
			print(Player.Name)
		else
			print("This is the current player.")
		end
	end
end)

since local current_player = tool.Parent.Parent will equal to nil.

1 Like

Thank you! I appreciate the help!