RemoteEvent.OnClientEvent:Connect() Not Working

I want to :FireClient(plr,arg1,arg2) a remote event every 0.25 seconds the player is in radius of a part (machine). Then I want the client to detect that and make the client’s UI TextLabel.Text = arg1. I got the first part working (firing the remote event), but the local script doesn’t detect it.

image
(Errors are not related to the problem)

Local Script in StarterGui:

local ws = game:GetService("Workspace")
local plrs = game:GetService("Players")
local uiService = game:GetService("UserInputService")
local rService = game:GetService("RunService")
local rStorage = game:GetService("ReplicatedStorage")
local hb = rService.Heartbeat
local plr = plrs.LocalPlayer
local char = plr.Character
local hrp = char:WaitForChild("HumanoidRootPart",10)

local mp = plr.PlayerGui.MachineProgress
local mw = plr.PlayerGui.MachineWarning
local machine_radius = 10
local machinesFolder = ws.Machines
local machines = machinesFolder:GetChildren()
local progressText = plr.PlayerGui.MachineProgress.Frame.TextLabel.Text

local mEvent = rStorage:WaitForChild("Machine",10)
local mUIEvent = rStorage:WaitForChild("ChangeMachineUI",10)

local pressedE = false


uiService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		pressedE = true
	end
end)

while wait(.1) do
	for index, machine in pairs(machines) do
		if (hrp.Position-machine:WaitForChild("Radio",10).Position).magnitude < machine_radius then
			mp.Enabled = false
			mw.Enabled = true
			if pressedE then
				mEvent:FireServer(true,machine)
				mp.Enabled = true
				mw.Enabled = false
			end
		else
			pressedE = false
			mw.Enabled = false
			mp.Enabled = false
			mEvent:FireServer(false,machine)
		end
	end
end

local function onRecieveMachineData(progress)
	print("client picked up")
	game.Players.LocalPlayer.PlayerGui.MachineProgress.Frame.TextLabel.Text = ""..progress.."%"
end

mUIEvent.OnClientEvent:Connect(onRecieveMachineData)

Server Script in ServerScriptService:

local rStorage = game:GetService("ReplicatedStorage")
local rService = game:GetService("RunService")
local mEvent = rStorage:WaitForChild("Machine",10)
local mUIEvent = rStorage:WaitForChild("ChangeMachineUI",10)
local hb = rService.Heartbeat
local increasingPercentage = false
local plr
local progressObj
local progress = 0
local machObj


function mEventFired(player, value, machineObj)
	plr = player
	machObj = machineObj
	if value == true then
		increasingPercentage = true
		progressObj = machineObj.Radio.Progress
		if progressObj.Value ~= 0 then
			progress = progressObj.Value
		end
	else
		increasingPercentage = false
	end
end

mEvent.OnServerEvent:Connect(mEventFired)

while wait() do
	if increasingPercentage == true and progressObj ~= nil and progress < 100 then
		mUIEvent:FireClient(plr, progress)
		print("fired client")
		wait(.25)
		progress = progress + 1
		progressObj.Value = progress
	else
		increasingPercentage = false
	end
end

The reason it is “not working” is because you are connecting the event after a while loop, which means the event never gets connected as a while loop is infinite (and yields the thread), something you can do to fix the error is connect the event before the while loop OR spawn a new thread around the while loop like this:

task.spawn(function()
    while wait(.1) do
	    for index, machine in pairs(machines) do
		    if (hrp.Position-machine:WaitForChild("Radio",10).Position).magnitude < machine_radius then
			    mp.Enabled = false
			    mw.Enabled = true
			    if pressedE then
				    mEvent:FireServer(true,machine)
				     mp.Enabled = true
				     mw.Enabled = false
			    end
		    else
			    pressedE = false
			    mw.Enabled = false
			    mp.Enabled = false
			    mEvent:FireServer(false,machine)
		    end
	    end
    end
end)
2 Likes

You’re running an infinite loop in your client script (the while wait() do thing), so it never gets to the line below it where the script connects to the remote. Edit: ninja’d