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

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

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)

1 comment

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

1 answer

✓ Accepted answer

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.

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