Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 1 year ago

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

Log in to vote
1
Answered by 1 year ago

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)
0
I didn't know you could fire to all clients. This helped alot, thanks! Uniplier 83 — 1y
Ad

Answer this question