Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to fire an event from a server script to a local script?

Right now I am attempting to make an ability that darkens and fogs up the screen of everyone in the server except players with the vampire role, and the ability user. I wrote up 2 scripts using bindableEvents. When the player presses a key, a local script sends an event that then fires the bindableEvent, Then a localscript placed inside all players will fog up the effected players game accordingly. The problem is, the serverscript is not firing the event correctly. I don't get any errors for it, when I fire the event through the localscript, it fires when players spawn in. If there is an alternative to this method, or a way to fix it, any help would be appreciated.

Heres the serverscript firing the event:

local BeastEvent = game.ReplicatedStorage.Events.Nightfall
local bindableEvent = game.ReplicatedStorage.Events:WaitForChild("Nightfell")

BeastEvent.OnServerEvent:Connect(function(player)
	for _, player4 in ipairs(game.Players:GetPlayers()) do
			bindableEvent:Fire(player4)
	end
end)

And the localscript:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local bindableEvent = ReplicatedStorage.Events:WaitForChild("Nightfell")
local char = script.Parent
local function OnEvent(player)
	if player.Character.charactervalues.Role.Value ~= "Vampire" then
	print("heya") --> Hello, world!
	end
end

bindableEvent.Event:Connect(OnEvent)

1 answer

✓ Accepted answer

When the Server wants to tell all players something, it should use RemoteEvent:FireAllClients().

In this specific scenario, use FireAllClients while sending the player who is a vampire, and have the players check individually if they are the vampire.

RemoteEvent.OnClientEvent:Connect(function(vamp)
	if vamp ~= localplayer
		--player should darken their screen because they are not the vampire
	end
end)

1 comment

0

I didn't know you could fire to all clients. This helped alot, thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.