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