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

I am using FireClient() but I don't know how to get the player??

Asked by
RootEntry 111
6 years ago

Hi. I am trying to do a Thirst and Hunger script.

This is inside my script:

game.Players.PlayerAdded:connect(function(player)
    while wait() do
        wait(20)
        script.SendSignal:FireClient(player)
    end
end

I have tried without player added, and without the player argument, and it didn't work. Then I tried with the player added, and nothing happened. I don't know how to define the player.

LocalScript:

local SSS = game:GetService('ServerScriptService')
local SendSignal = SSS.HungerScript.SendSignal
local player = game:GetService('Players').LocalPlayer

SendSignal.OnClientEvent:connect(function()
    if script.Parent.Hunger.Value >= 0 then
        script.Parent.Hunger.Value = script.Parent.Hunger.Value -1
    end
    if script.Parent.Hunger.Value <= 0 then
        repeat
            wait(5)
            player.Character.Humanoid.Health = player.Character.Humanoid.Health -2
        until script.Parent.Hunger > 0
    end
end)
0
The first script will work fine, are you sure you waited 20+ seconds before confirming it didn't work? As for the second script why not put the event in replicatedstorage? ServerScriptService will not replicate to clients Vulkarin 581 — 6y

1 answer

Log in to vote
0
Answered by
mattscy 3725 Moderation Voter Community Moderator
6 years ago

Local scripts cannot see inside ServerScriptService or ServerStorage. Instead, all RemoteEvents and RemoteFunctions should be stored inside ReplicatedStorage.

You should put the remote event in ReplicatedStorage and do it like this instead:

game.Players.PlayerAdded:connect(function(player)
    while wait() do
        wait(20)
        game.ReplicatedStorage.SendSignal:FireClient(player)
    end
end
local RS = game.ReplicatedStorage
local SendSignal = RS.SendSignal
local player = game:GetService('Players').LocalPlayer

SendSignal.OnClientEvent:connect(function()
    if script.Parent.Hunger.Value >= 0 then
        script.Parent.Hunger.Value = script.Parent.Hunger.Value -1
    end
    if script.Parent.Hunger.Value <= 0 then
        repeat
            wait(5)
            player.Character.Humanoid.Health = player.Character.Humanoid.Health -2
        until script.Parent.Hunger > 0
    end
end)

As well as this, sending a signal over to the client at regular intervals may take up unnecessary bandwidth. It shouldn’t be too much of a problem with intervals of that length, but a better practice may be to have the loop on the client instead of the server.

Ad

Answer this question