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

Why wont this script enable the player gui when told?

Asked by 4 years ago

So I wrote out this code, but for some reason it won't work.

game.Players.PlayerAdded:Connect(function()
    local Player = game.Players.LocalPlayer
    if Player:IsInGroup(4324101) then
        Player.PlayerGui.Spawn.Enabled = true
    end
end)

Any ideas as to why? The script is a local script and no output errors.

0
PlayerAdded won't fire for the local player because by the time the script is locally executed the player has already joined the game. Why do you need line 1 and 6? Remove them. pidgey 548 — 4y
0
You do realize this could have been done in a single statement: `Player.PlayerGui.Spawn.Enabled = Player:IsInGroup(4324101)` Still not secure though, exploiters can easily enable it even if they're not in the group. Can't find a way to stop them. User#24403 69 — 4y
0
Oof, thanks! iiBuilder_Boy 27 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

You do not necessarily need the PlayerAdded event here at all. Since you are using a local script to check whether the LocalPlayer is in a group, you can simply do just that:

local player = game.Players.LocalPlayer

if player:IsInGroup(4324101) then
    player.PlayerGui.Spawn.Enabled = true
end

The issue with your current set up is that the PlayerAdded event may not fire after the local script is placed properly. Additionally, the function passed in Connect will also be called every time a new player joins, which may not be desirable.

Ad

Answer this question