Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
DesertusX 435 Moderation Voter
4 years ago

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.

0
PlayerGui, not StarterGui. Ziffixture 6913 — 4y
0
Thank you :) I will try DesertusX 435 — 4y
0
Strange, still doesnt do anything DesertusX 435 — 4y
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 Block_manvn 395 — 4y
View all comments (2 more)
0
How could i do that if there are 2 players? DesertusX 435 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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

0
I'm getting an error in the output which says "OnServerEvent can only be used in the server" from the LocalScript. DesertusX 435 — 4y
0
I fixed it now. LinavolicaDev 570 — 4y
0
thx DesertusX 435 — 4y
Ad

Answer this question