Maybe because there isn’t 2 or more players.
The LocalScript code I gave you was an example code which you were supposed to put into a new script parented to the gui and change it according to your needs. You still need to have the one which notifies the server on part touch, though you should rather handle the touch on the server altogether.
I tested the scripts in test mode with 2 players so that was not the issue
But when the script started, was there 2 players in the game? Maybe one loads after the other. Put that line in the function, as I said earlier.
First of all you need to replace
game.Players:GetPlayers(hit.Parent)
withgame.Players:GetPlayerFromCharacter(hit.Parent)
, as mentioned above.Second of all, StarterGui is client-sided. Changing the text there from the server will have no effect on the players. What you need to do is make the server use
FireAllClients
to tell the clients to change the text in their PlayerGui, not StarterGui .Ideally, you should parent the LocalScript to the gui in StarterGui and do something like this:
local gui = script.Parent local winnergui = gui.TextLabel local remote = game.ReplicatedStorage:WaitForChild("showtext1") remote.OnClientEvent:Connect(function(winner) winnergui.Text = "The winner is ".. winner winnergui.BackgroundTransparency = 0 wait(5) winnergui.BackgroundTransparency = 1 end)
and the server:
local replicatedstorage = game:GetService("ReplicatedStorage") local remoteevent = replicatedstorage:WaitForChild("showtext1") remoteevent.OnServerEvent:Connect(function(player, winnergui, hit) local winner = game.Players:GetPlayerFromCharacter(hit:FindFirstAncestorOfClass("Model")) if winner then remoteevent:FireAllClients(winner.Name) end end)
This is exactly what you want to do, however, your problem is that Workspace is being held as the so-called “winner” for the program.
When the touched event is used, it will fire when any part touches it, even when that part is not a player.
if you want to create the script so that it detects only Players, then make sure you replace the server script in Kriot22’s example:
local replicatedstorage = game:GetService("ReplicatedStorage")
local remoteevent = replicatedstorage:WaitForChild("showtext1")
remoteevent.OnServerEvent:Connect(function(player, winnergui, hit)
local winner = game.Players:GetPlayerFromCharacter(hit:FindFirstAncestorOfClass("Model"))
if winner then
remoteevent:FireAllClients(winner.Name)
end
end)
with something simple, that checks to see if a player touched it:
local replicatedstorage = game:GetService("ReplicatedStorage")
local remoteevent = replicatedstorage:WaitForChild("showtext1")
local touched1 = game.Workspace.parts:WaitForChild("testpart")
touched1.Touched:Connect(function(part)
if part.Parent:FindFirstChild("Humanoid") then
remoteevent:FireAllClients(part.Parent.Name)
end
end)
that should solve your problem with workspace being held as the replicated “winner”.
Also, if you want to improve your code, please be sure to incorporate a debounce, so that the touched event doesnt spam the :FireAllClients() multiple times in one touch
if you dont know what debounce is, please check this article: Documentation - Roblox Creator Hub