Replicated to One Player Only

I’ll make this quick. I have a script that handles a flashlight, and thanks to the F for Flashlight script, I was able to get it working properly. When I tested the game with some others, surprisingly, the flashlight doesn’t work.

It replicates to one person, and then, when another tries to use the flashlight, it doesn’t. It only works on one person, and it’s baffling me…

For the script, I followed the link step-by-step, so everything in there is carbon-copied into mine.

Here is an error I’ve received:

your flashlight code is in a LocalScript so it only runs for one player,
move it into a Script e.g. in ServerScriptService to affect everyone.
Players.PlayerAdded > CharacterAdded and then character:WaitForChild("FlashHandle")
so you wait for the handle under each players character, the error you received means WaitForChild("FlashHandle") never found that object, double check you’re cloning/parenting FlashHandle into every character

The thing I don’t get is the fact that the RemoteEvent should have taken care of the whole “affecting only one player” thing.

I’ll see what I can do to transfer the script to a ServerScript, however.

Clone FlashHandle on the server for each players character using

game.Players.PlayerAdded:Connect(function(p)
  p.CharacterAdded:Connect(function(char)
    local flash = game.ReplicatedStorage.FlashHandle:Clone()
    flash.Parent = char
  end)
end)

RemoteEvents only send messages, they don’t auto clone or move models,
keep the RemoteEvent solely for toggling the light and on the server do;

toggleRE.OnServerEvent:Connect(function(player, on)
  toggleRE:FireClient(player, on)
end)

and on the client listen with OnClientEvent don’t use it to parent parts

Lastly, how do I make it so that EVERYONE in the server can see the flashlight a client has?

Use Remote events, that’s simple.

To add more to what is said above: send a remote event to the server with the variables (Flashlight, on/off), then use FireAllClients to notify all clients and update their flashlight state.

Would it be better to fire it with ALL clients or just one client?

What I have is a FireClient with the player and a boolean passed through to a local script (which holds the flashlight mechanic)

EquipFlash.OnServerEvent:Connect(function(player, trigger)
	EquipFlash:FireClient(player, trigger)
end)

I mean If you are wanting all players to see the light turn on/off, you would want to fire to all clients.

Here is a snippet of a flashlight I had done.

A client would fire to the server with isLightOn being a boolean and lightBulb is the light itself I want to turn on.

LightSignal:FireServer(isLightOn, lightBulb)

Server would read and pass back the light we want to enable/disable to all clients

LightSignal.OnServerEvent:Connect(function(player, isLightOn, lightBulb)
	
	-- Broadcast the state change to all clients
	LightSignal:FireAllClients(player, isLightOn, lightBulb)
end)

Then receive back the input on clientside and apply the change

LightSignal.OnClientEvent:Connect(function(player, isLightOn, LightBulb)
	-- Update the lamp's state locally based on the server's message
	if isLightOn then
		LightBulb.Enabled = true
		--bulb.Material = Enum.Material.Neon
		-- Any other client-side logic
	else
		LightBulb.Enabled = false
		--bulb.Material = Enum.Material.Ice
		-- Any other client-side logic
	end
end)

Hope that helps a bit.

1 Like

For the player instance inside the FireAllClients, does it mean that the flashlight will only turn on for THAT specific client? Meaning it won’t duplicate for everybody, but they can see the flashlight the client has?

EDIT: Just tested it with actual players, it just made it WORSE. Gonna try with FireClient instead.

Just like the other responses, sending a RemoteEvent signal → FireAllClients() is a good way. However, I foresee an issue where new players will not see flashlights enabled. You have to use stateful behavior for each player’s flashlight, so that when a new player joins, they can obtain that data and it would set the correct flashlight state for all players that they see.

2 Likes

FireAllClients means allClients will receive the determined message from the server. FireClient usually is used for when you want only ONE specific client to receive that message. Show me the script because fireAllClients in this case would be way better to use.

Local Script:

EquipFlash.OnClientEvent:Connect(function(player, trigger)
	--Simplified script for readibility
	if trigger then
		Flashlight = ReplicatedStorage.FlashHandle:Clone()
		Flashlight.Parent = Char
	else
		Flashlight:Destroy()
		Flashlight = nil
	end
end)

ServerScript:

EquipFlash.OnServerEvent:Connect(function(player, trigger)
	--Just showing points here at the bottom, this ain't the actual script

	EquipFlash:FireAllClients(player, trigger)
	--If I were to use FireAllClients, then EVERYONE will receive the flashlight

	EquipFlash:FireClient(player, trigger)
	--Using this, other players can SEE the animation, but they won't SEE the flashlight
end)

Using fireAllClients and then FireClient will result in firing all the clients + the client who sent the information. Thats not what we want

Nevermind, I am dumb. I didnt notice it was a placeholder

All good, guess it wasn’t that clear :sob:

1 Like

Whats the problem actually? I couldnt understand wheres the problem

Basically, if I use FireAllClients, then it makes it so that EVERYONE receives the flashlight, meaning the flashlight would turn on or off for EVERYBODY, including the person who activated the event.

If I use FireClient, however, I get the desired effect for the client who activated it, but everyone else won’t see the flashlight they’re holding, let alone the light that emits from the flashlight.

something you can do is a for loop including all players (game.Players:GetPlayers()), and then check if it is the local player, you do what u want for ONLY the client. It is anyone else, u fire client with the player iterated doing what u want for them

for _, plr in game.Players:GetPlayers do
if plr == plrThatActivatedFlashlight then … return end
event:FireClient(plr, …)

end