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)
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?
Change the script type to Server Script and place it in ServerScriptService.
In TriggerPart, insert the path to your part (if you want to change it).
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)
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)