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

Why Does My GUI Enabling Script Returns Nil?

Asked by 5 years ago

For some reason it isn't working, it says "trying to index a nil value PlayerGui."

I checked and everything is working except 'game.Players.name.PlayerGui.BoatSpawn.Enabled = true'

I also checked what 'name' returns and it is working.

local Part = script.Parent

Part.Touched:connect(function(HIT)
local H = HIT.Parent:FindFirstChild("Humanoid")
 if H then
  local Player = game.Players:GetPlayerFromCharacter(HIT.Parent)
  local name = H.Parent.Name
  game.Players.name.PlayerGui.BoatSpawn.Enabled = true
 end
end)

It is in a script in a part in the workspace.

0
is this a server script? RetroGalacticGamer 331 — 5y

1 answer

Log in to vote
0
Answered by
xPolarium 1388 Moderation Voter
5 years ago
Edited 5 years ago

The problem is how you use variable name to get the player. To properly index you use [ ] brackets after the parent you're indexing.

On line 08 it should be:

game.Players[name].PlayerGui.BoatSpawn.Enabled = true

There are two problems with this. One being that you're trying to access something in playergui through a server Script. Server scripts do not have access to the PlayerGui so you will need to use a local script to edit BoatSpawn if it's a ScreenGui or GuiObject.

The second problem is,

Why index the player when you get them on line 06 using :GetPlayerFromCharacter()? You can simplify your code by removing lines 07 and 08:

local Player = game.Players:GetPlayerFromCharacter(HIT.Parent)
--Player.PlayerGui.BoatSpawn.Enabled = false

Again, the first problem still stands here since you should have the client handle the Gui. See RemoteEvents if you want to see how to get by this.

Edit:

The reason you need a RemoteEvent here is because the server cannot see the ScreenGui in the PlayerGui. You need to communicate with the client (Which does see the Gui) to change it's properties.

To go about this you will need a RemoteEvent in ReplicatedStorage, the same Touched script I gave you and a local script in the ScreenGui we're editing.

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remoteEvent = ReplicatedStorage.RemoteEvent

local Part = script.Parent

local debounce = false

Part.Touched:Connect(function(HIT)
    if debounce then return end
    debounce = true
    local human = HIT.Parent:FindFirstChildOfClass("Humanoid")
    if human then
        local Player = game.Players:GetPlayerFromCharacter(HIT.Parent)
        remoteEvent:FireClient(Player)
        wait(3)
    end
    debounce = false
end)

I included a debounce to prevent event spam.

Then in the client we listen for the RemoteEvent to go off using OnClientEvent.

--local script in ScreenGui we want to toggle

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")

local screenGui = script.Parent --The parent of this script should be the ScreenGui

--not is a handy substitute here so we don't use messy if-statements.
local function onToggleGui()
    screenGui.Enabled = not screenGui.Enabled
end

remoteEvent.OnClientEvent:Connect(onToggleGui) --Connect to the function
0
You cant italicize brackets or they look ridiculous lol. DinozCreates 1070 — 5y
0
Yes, they look italicized. xPolarium 1388 — 5y
0
Use the inline code brackets instead, its gross. DinozCreates 1070 — 5y
0
I didn't like how that looked like either. xPolarium 1388 — 5y
View all comments (3 more)
0
I did what you said in a local script and now it says that BoatSpawn is not a valid member of PlayerGui (even though it is). Also since it is just the visibility of a GUI shouldn't it work without remotes? xXGokyXx 46 — 5y
0
@xPolarium - Can you show me an example, I'm still having issues. xXGokyXx 46 — 5y
0
Included example as asked. xPolarium 1388 — 5y
Ad

Answer this question