-- 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)
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?
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.
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.