2 players getting GUI separitly

Hello im making a script where when one player touchs a part he get a local screen gui and 2 seconds later the second player get a screen gui too but theres only the local screenGui that is activating idk why

game.Workspace.part.touched:connect(function(Player)
local player = game:GetService("Players"):GetPlayerFromCharacter(Player.Parent) if player then
if Player.Parent:FindFirstChild("Humanoid") then
player.PlayerGui.ScreenGui.Enabled = true
wait(2)
game.StarterGui.ScreenGui.Enabled = true
wait(5)
player.PlayerGUI.ScreenGui.Enabled = false
game.StarterGui.ScreenGui.Enabled = true
end
end
end)

What type of script are you using? Local or server?

I’m using a Local script///////////////////

Explain to me what “second player get a screen gui” means? Does your game have a maximum of 2 players or should all players on the server see ScreenGui after the first player?

The game have a maximum of 2 players so im using the server gui to make him see the gui

Why not send a remote event with the touched player and you can get the localplayer from the local script?

I dont have any idea on how to do that im new to scripting if you could help me i would be grateful

  1. Change the script type to Server Script and place it in ServerScriptService.
    image

  2. In TriggerPart, insert the path to your part (if you want to change it).
    image

  3. Put this code into created script

local Players = game:GetService("Players")
local TriggerPart = workspace.Part -- way to part

local function ToggleScreenGui(Player: Player, ScreenGuiName: string, State: boolean): nil
	Player.PlayerGui[ScreenGuiName].Enabled = State
end

TriggerPart.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local FirstPlayer = Players:GetPlayerFromCharacter(hit.Parent)
		local SecondPlayer = if Players:GetPlayers()[1] == FirstPlayer then Players:GetPlayers()[2] else Players:GetPlayers()[1]
		
		ToggleScreenGui(FirstPlayer, "ScreenGui", true)
		
		task.wait(2)
		
		ToggleScreenGui(SecondPlayer, "ScreenGui", false)
		
		task.wait(5)
		
		ToggleScreenGui(FirstPlayer, "ScreenGui", false)
		ToggleScreenGui(SecondPlayer, "ScreenGui", false)
	end
end)

It says this


Don’t change the ToggleScreenGui function.

local function ToggleScreenGui(Player: Player, ScreenGuiName: string, State: boolean): nil
	Player.PlayerGui[ScreenGuiName].Enabled = State
end

ScreeGuiName is an argument that is passed to the function when it is called, so nothing needs to be changed.

ToggleScreenGui(FirstPlayer, "ScreenGui", true)

Change “ScreenGui” to your screen gui’s name but don’t remove the quotes.


And one more thing, when you are alone on the server, the script will not work, since there is no second player.

If you don’t want to see the error, insert this script instead of the old one.

local Players = game:GetService("Players")

local TriggerPart = workspace.Part

local function ToggleScreenGui(Player: Player, ScreenGuiName: string, State: boolean): nil
	if Player then
		Player.PlayerGui[ScreenGuiName].Enabled = State
	end
end

TriggerPart.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local FirstPlayer = Players:GetPlayerFromCharacter(hit.Parent)
		local SecondPlayer = if Players:GetPlayers()[1] == FirstPlayer then Players:GetPlayers()[2] else Players:GetPlayers()[1]
		
		ToggleScreenGui(FirstPlayer, "ScreenGui", true)
		
		task.wait(2)
		
		ToggleScreenGui(SecondPlayer, "ScreenGui", false)
		
		task.wait(5)
		
		ToggleScreenGui(FirstPlayer, "ScreenGui", false)
		ToggleScreenGui(SecondPlayer, "ScreenGui", false)
	end
end)

It is actually working thank you very much !

1 Like

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