RemoteEvent not firing properly from a script to a localscript in ScreenGUI

I’m having issues with my Script not firing properly to my localscript.
Script:

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")

local checkpoint = script.Parent

function onTouched(hit)
	if hit and hit.Parent and hit.Parent:FindFirstChildOfClass("Humanoid") then
		
		local RS = game.ReplicatedStorage

		local levelDef = RS.level

		levelDef.Value = 1

		print(levelDef.Value)
		
		local remoteEvent = RS:FindFirstChildOfClass("RemoteEvent")

		local function updateUI()
			print("fire")
			remoteEvent:FireClient()
		end
		Players.PlayerAdded:Connect(updateUI)
		
		----- the rest of the script (unimportant)

localScript

local RS = game.ReplicatedStorage
local levelDef = RS.level.Value
local levelName = script.Parent.LName
local levelNumber = script.Parent.Number

local Players = game:GetService("Players")

local remoteEvent = RS:FindFirstChildOfClass("RemoteEvent")

local player = Players.LocalPlayer

local function updateUI()
	--[[
	
	normally it says:
	
	"uh oh"
	"something went wrong"
	
	as the default text on the TextLabel
	
	]]--
	
	if levelDef == 0 then
		levelName.Text = "erm"
		levelNumber.Text = "the game's kinda working but not properly!"
	end

	if levelDef == 1 then
		levelName.Text = "Level Name"
		levelNumber.Text = "Level Number"
	end
end

remoteEvent.OnClientEvent:Connect(updateUI)

And a recording showing that level == 1 however the event isn’t firing. This is also obvious due to it not printing “Fire”
[recording failed to upload][will retry soon]

the UI still says “uh oh” and doesnt even go to “erm”

1 Like

Screenshot 2024-01-14 at 3.56.12 PM

this is everything in replicated storage

2 Likes

You should probably pick another event, this will obviously not work since it won’t run until another player joins

You need to include the player in the first argument. You can get the player from a character model like this:

local player = game.Players:GetPlayerFromCharacter(hit.Parent)
--...
remoteEvent:FireClient(player)

This saves the variable as a normal number and won’t change. Just get the Number/IntValue object then get .Value of it whenever you need checking.

2 Likes

yeah i’m not so sure what to use i’m new to RemoteEvents and I was following a page on roblox website

I’ll try

local levelDef = RS.level
•••

	if levelDef.Value == 0 then
		levelName.Text = "erm"
		levelNumber.Text = "the game's kinda working but not properly!"
	end

like this?

1 Like
function onTouched(hit)
	if hit and hit.Parent and hit.Parent:FindFirstChildOfClass("Humanoid") then
		
		-----
		local player = Players:GetPlayerFromCharacter(hit.Parent)
		
		local RS = game.ReplicatedStorage

		local levelDef = RS.level

		levelDef.Value = 1

		print(levelDef.Value)
		
		local remoteEvent = RS:FindFirstChildOfClass("RemoteEvent")

		local function updateUI()
			print("fire")
			remoteEvent:FireClient(player)
		end
		Players.PlayerAdded:Connect(updateUI)
		
		----- eos
1 Like

Yes

It really depends on what you update. Use connections that relate to the things that will change and needs updating (for now it is only level so connect the .Changed event of the level object)

2 Likes

I don’t really know how to do that. Do you think there is a page which could help me or could you explain how I could try and implement this?

based on what you said and what i understood:

levelDef.Value.Changed:Connect(updateUI) although this is probably wrong.

1 Like


yeah uh that was definitely wrong :skull:

1 Like

Use it on the object, not the value like this: levelDef.Changed:Connect(updateUI)

1 Like

It no longer errors but the UI still doesn’t update.

could it be an issue with the localScript?

1 Like

Have you tried this yet?
Try firing the remoteEvent once outside updateUI function on the server script, .Changed might not get fired

I did it and it worked!

Thanks!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.