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

I have a problem of getting the player's playergui?

Asked by 6 years ago

I'm making a shackle tool and I need to access the prisoner's playerGui when I click on him with the shackles. I'll mark the place where I'm having troubles. Could you help me figure out what's wrong?

local shackles = script.Parent
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local canJail = false
local studsToArrest = 20

function onEquipped()
    print("Shackles equipped")
    canJail = true
end

function onUnequipped()
    print("Shackles unequipped")
    canJail = false
end

function onActivation()
    if mouse.Target.Parent:FindFirstChild("Humanoid") then
        local prisoner = mouse.Target.Parent
        local mag = (prisoner.Torso.Position - plr.Character.Torso.Position).magnitude
    if mag <= studsToArrest then
        if prisoner:FindFirstChild("timesJailed") then
            prisoner.timesJailed.Value = prisoner.timesJailed.Value + 1
        else
            local tj = Instance.new("NumberValue", prisoner)
            tj.Name = "timesJailed"
            tj.Value = 1
        end

        if game.Workspace.Jail.jailSpawn1.Occupied.Value == false then
            prisoner.Torso.CFrame = game.Workspace.Jail.jailSpawn1.CFrame + Vector3.new(0,2,0)
            game.Workspace.Jail.jailSpawn1.Occupied.Value = true
        elseif game.Workspace.Jail.jailSpawn2.Occupied.Value == false then
            prisoner.Torso.CFrame = game.Workspace.Jail.jailSpawn2.CFrame + Vector3.new(0,2,0)
            game.Workspace.Jail.jailSpawn2.Occupied.Value = true
        elseif game.Workspace.Jail.jailSpawn3.Occupied.Value == false then
            prisoner.Torso.CFrame = game.Workspace.Jail.jailSpawn3.CFrame + Vector3.new(0,2,0)
            game.Workspace.Jail.jailSpawn3.Occupied.Value = true
        elseif game.Workspace.Jail.jailSpawn4.Occupied.Value == false then
            prisoner.Torso.CFrame = game.Workspace.Jail.jailSpawn4.CFrame + Vector3.new(0,2,0)
            game.Workspace.Jail.jailSpawn4.Occupied.Value = true
        elseif game.Workspace.Jail.jailSpawn5.Occupied.Value == false then
            prisoner.Torso.CFrame = game.Workspace.Jail.jailSpawn5.CFrame + Vector3.new(0,2,0)
            game.Workspace.Jail.jailSpawn5.Occupied.Value = true
        elseif game.Workspace.Jail.jailSpawn6.Occupied.Value == false then
            prisoner.Torso.CFrame = game.Workspace.Jail.jailSpawn6.CFrame + Vector3.new(0,2,0)
            game.Workspace.Jail.jailSpawn6.Occupied.Value = true
        else
            print("All prison cells are full")
            if plr.PlayerGui:FindFirstChild("Notifier") then
                if plr.PlayerGui.Notifier == false then
                    plr.PlayerGui.Notifier.Enabled = true
                    for i = 1,0,-0.1 do
                        plr.PlayerGui.Notifier.notifier.TextTransparency = i
                        wait()
                    end
                    wait(2)
                    for i = 0,1,0.1 do
                        plr.PlayerGui.Notifier.notifier.TextTransparency = i
                        wait()
                    end
                end
            end
        end

        print(prisoner.Name.." has been jailed "..prisoner.timesJailed.Value.." times now...")
        game.Players:GetPlayerFromCharacter(prisoner).TeamColor = BrickColor.new("Really black")

        if prisoner.timesJailed.Value == 1 then
            local prisonerPlr = game.Players:GetPlayerFromCharacter(prisoner)
            for i,v in pairs(prisonerPlr:GetChildren()) do
                print(v.Name) --I print out the children here
                wait()
            end
            prisoner.PlayerGui.JailTimer.Frame.Visible = true --The error says playerGui doesnt exist in the player.
            for i = 120,0,-1 do
                print(i)
                prisonerPlr.PlayerGui.JailTimer.Frame.Timer.Text = i
                wait(1)
            end
        end
    end
    end
end

shackles.Equipped:connect(onEquipped)
shackles.Unequipped:connect(onUnequipped)
shackles.Activated:connect(onActivation)

2 answers

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

If I am correct it seems that you are trying to find inside of a player's CHARACTER

you see... Guis are contained in the player, not character

prisonerPlr.PlayerGui.JailTimer.Frame.Visible = true

see how i made it prisonerPlr?

It was a simple mistake really....

Ad
Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
6 years ago
Edited 6 years ago

To explain the error, you weren't accessing the PlayerGui from the 'prisonerPlr' variable.

But unfortunately if FilteringEnabled is enabled as it should be, you cannot directly manipulate anything on the client. You must use remotes.

  • Setup a RemoteEvent in ReplicatedStorage.

  • Use FireClient from the server.

  • Change the UI's visibility in an OnClientEvent event from the client.

Server:

local remote = game.ReplicatedStorage.TimerRemote --This is your RemoteEvent

--codecodecode

if prisoner.timesJailed.Value == 1 then
    local prisonerPlr = game.Players:GetPlayerFromCharacter(prisoner)
    --print children
    if prisonerPlr then --Make sure they exist
        remote:FireClient(prisonerPlr) --FireClient
    end
end          

Client:

'frame' should be equivalent to what was prisonerPlr.PlayerGui.JailTimer.Frame
local remote = game.ReplicatedStorage.TimerRemote --This is your RemoteEvent
local frame = script.Parent
local timer = frame:WaitForChild("Timer")

remote.OnClientEvent:Connect(function() --OnClientEvent
    frame.Visible = true
    for i = 120,0,-1 do
        print(i)
        timer.Text = i
    end
    frame.Visible = false
end)

Answer this question