Why isn't this Remote Event working?

Hello there! :grinning:

  1. What do you want to achieve? Keep it simple and clear!
    I want to create a Remote Event so when there is 2 players in the game and someone touches the part called “testpart” it would display the winner for everyone not just the winner and give the winner 1 win in the leader board.

  2. What is the issue? Include screenshots / videos if possible!
    The issue is that the Remote Event isn’t working and it doesn’t even say who won for the winner himself now which means that the entire script isn’t working and there is no errors in the output which is weird, either something is preventing the script from working or it’s being skipped completely but I don’t know what’s wrong.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried to look online on Youtube and the Developer API website but I can’t fix the problem still and I am new to Remote Events/Remote Functions so sorry if the problem is obvious.

Setting up the Remote Event when the Part is touched
repeat
	
until
#game.Players:GetPlayers() >= 2

print("wooosh")
local testpart = game.Workspace.parts:WaitForChild("testpart")

game.Workspace.parts.RealPart.Touched:Connect(function(hit)
	if game.Players:GetPlayerFromCharacter(hit.Parent) then
		print("hehe boi")
		game.ReplicatedStorage.RemoteEvent:FireServer(hit)	
	end
end)
Firing the Remote Event
repeat
	wait()
until
#game.Players:GetPlayers() >= 2

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Wins = Instance.new("IntValue", leaderstats)
	Wins.Name = "Wins"
	Wins.Value = 0

	game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player,hit)
		print("remote event fired by: "..tostring(hit.Parent))
		Wins.Value = Wins.Value + 1
		player.PlayerGui.winner.TextLabel.Text = "The winner is "..tostring(hit.Parent)
		player.PlayerGui.winner.TextLabel.TextTransparency = 0
		wait(5)
		player.PlayerGui.winner.TextLabel.TextTransparency = 1
		print("testing")
	end)
end)
print("the remote event has been skipped/???")
1 Like

I believe the problem is in your client script, you are yielding the entire script until after 2 players join, and then setting up a PlayerAdded which won’t fire until another player joins.

In your server script, is hehe bois actually being printed?

Yeah, hehe boi is being printed which means that the Remote Event set up is working but the event listener is being skipped/stopped from working completely due to something that I don’t know about.

1 Like

Okay that makes sense, then the issue is definitely with your client script.

Let me explain this;

When you run:

repeat
	wait()
until
#game.Players:GetPlayers() >= 2

You are literally yielding the entire script, preventing anything else from running.

After 2 players are in the game, the rest of your code will run and set up the PlayerAdded events, since the players were already in the game, that won’t fire and your remote listeners will not be connected.

But I want the script to run once there are 2 or more players in the game is there any other solution that constantly checks if there are 2 or more players in the game than removing the repeat until loop?

1 Like

So, I think you should create your leaderstats on the server, and have the client only listen to the OnServerEvent.

Then have the server check if anyone touches the part and see if there’s two players currently in the game, if there are then fire back to them.

Great idea! I will try that and then update you back if it works.

Hold on, I got mixed up on the order of your scripts. I thought your server script was your client one. My bad.

All you need to do is change this to this:

print("wooosh")
local testpart = game.Workspace.parts:WaitForChild("testpart")

game.Workspace.parts.RealPart.Touched:Connect(function(hit)
	if #game.Players:GetPlayers() >= 2 and game.Players:GetPlayerFromCharacter(hit.Parent) then
		print("hehe boi")
		game.ReplicatedStorage.RemoteEvent:FireServer(hit)	
	end
end)

And then just check once the server is fired, if there’s 2 players.

1 Like

It’s ok is there anything different that I have to do now?

1 Like

it repeats nothing, it should be:

repeat
wait()
until #game.Players:GetPlayers() >= 2

try print debugging, also… the second player can leave right after and the game could start with one player and one player only, also, please tell us what prints in the output and what doesn’t

I have edited my previous post to correct my mistake, please take a look.

Alright got it, I will test it now.

1 Like

Okay so something weird happened…

It printed hehe boi and everything was good but at the start of the game in roblox studio before the test mode players even spawned in it instantly printed “the remote event has been skipped/???” which will only happen if the Remote Event has been skipped over completely but the thing is that the player that touched the part can see the GUI saying that they won but the other player(s) can’t.

The print will fire no matter what, using :Connect on something spawns it in a new thread, and will not yield your script.

If you had a script like:

brick.Touched
print("This will print")

The touched event will still listen, but the print will also output.

As for your other issue, the reason why the player who touched it can see is it because the first argument of OnServerEvent is the player who fired it.

1 Like

So the argument “player” is only referencing to the player that touched the Part and not everyone? If so I believe if I get all the players in the game in the event listener and then put that as an argument it would work right?

1 Like

I would advise against doing that, and to my knowledge you shouldn’t actually be able to access anything in PlayerGui from the server.

What I think you should ultimately do is have the touched event (with the player check) on the server, then the server increases the wins leaderstats for that player (which will replicate to everyone) and then fires a RemoteEvent to every client which will tell them to update their GUI.

That’s the most secure you can be, and to my knowledge should work great.

Alright but here’s some notes if you wanted them: the Remote Event set up with the touched event is in a local script inside StarterGUI inside the TextLabel which is the winner GUI and the Remote Event Fire is in a script in ServerScriptService

2 Likes

Right, but this whole organization seems wrong. Why have the client detect who touched it, and then tell the server? Just have the server check who touched it.

1 Like

Okay I will try and fix it based on your responses