Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why are none of the commands under this if statement not running?

I made a script to wait until there are two people in the server. None of the commands under this if statement are working. Can anyone please point out the problem. Just so you know, this is in a ServerScript in ServerScriptService. Thank you for helping.

local plrs = game.Players:GetPlayers()
local ws = workspace
local gui = game.StarterGui

if #plrs == 1 then --I changed it to 1 so I could test it alone.
	print"started"
	ws.RedCage:Destroy()
	ws.BlueCage:Destroy()
	gui.ScreenGui.Frame.PreGame.Visible = false
	gui.ScreenGui.Frame.Timer.Visible = true
	gui.ScreenGui.Frame.BluePlayer.Visible = true
	gui.ScreenGui.Frame.RedPlayer.Visible = true
end

Thank you for your time.

6 comments

0

PlayerGui, not StarterGui.

0

Thank you :) I will try

0

Strange, still doesnt do anything

0

PlayerGui is parent-ed in Player. In the case there is only one Player, we can do like this to get the PlayerGui: local gui = plrs[1].PlayerGui

0

How could i do that if there are 2 players?

1 answer

✓ Accepted answer

The GUI should be player's gui, and it is parented to the player. And your code should be inside the local script. As far as that your code is only inside this script, it would be complicated to fix your code.

--This should be the normal script, put it inside Server Script Service
local plrs = game.Players:GetPlayers()
local Event = Instance.new("RemoteEvent",workspace)
Event.Name = "GuiEvent"
--[[IF YOU WANT TO DELETE THE CAGES GLOBALLY
local deleted = false
--]]
if #plrs == 1 then --I changed it to 1 so I could test it alone.
    print"started"
    --If you want to test it to yourself, then I will only use one individual inside this statement.
    local PlrGui = plrs[1]:WaitForChild("PlayerGui")
    Event:FireClient(plrs[1])--Fire the event
	--[[IF YOU WANT TO DELETE THE CAGES GLOBALLY
	if deleted == false then
	ws.RedCage:Destroy()
    	ws.BlueCage:Destroy()
	deleted = true
	end--]]
end
--===================
--Local Script
local Event = game.Workspace:WaitForChild("GuiEvent")
local gui = game.Players.LocalPlayer:WaitForChild("PlayerGui")
--[[IF YOU WANT TO DELETE THE CAGES ON THE CLIENT SIDE
local deleted = false
--]]
Event.OnClientEvent:Connect(function()
    gui.ScreenGui.Frame.PreGame.Visible = false
    gui.ScreenGui.Frame.Timer.Visible = true
    gui.ScreenGui.Frame.BluePlayer.Visible = true
    gui.ScreenGui.Frame.RedPlayer.Visible = true
	--[[IF YOU WANT TO DELETE THE CAGES ON THE CLIENT SIDE
	if deleted == false then
	ws.RedCage:Destroy()
    	ws.BlueCage:Destroy()
	deleted = true
	end--]]
end)

Further details: Client-Server Model, Remote Functions and Events

Edit 2020/4/24 20:36 GMT+8 : Fixed typo of the code

3 comments

0

I'm getting an error in the output which says "OnServerEvent can only be used in the server" from the LocalScript.

0

I fixed it now.

0

thx

This discussion is preserved as it was. New answers, comments, and votes are closed.