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

Attempted to index nil with playergui?

Asked by 1 year ago

CODE

c = workspace.htisguy.Sound

o = game.Players.LocalPlayer

cc = game.workspace.htisguy

k = game.SoundService.Sound

cc.Touched:Connect(function() 
    k:Pause()
    c:Play() 
    task.wait(0.5) 
    o.PlayerGui.warp.Enabled = true 
end)

EXPLANATION

I am attempting to make a sound play and after a bit, a gui will enable turning the screen black.

In the game, falling into a specific pit will trigger this event.

Please ignore the horrible variable names. I don't organize alot of my stuff, besides I do only small scripts

ERRORS

Workspace.htisguy.Script:16: attempt to index nil with 'PlayerGui'

MORE

I am using a serversided script.

Using a local script does NOT work.

0
those are some epic variables LikeableEmmec 470 — 1y

2 answers

Log in to vote
0
Answered by 1 year ago

@virushunter9 is correct! To get the people who fall into the pit, you can get the part that touched cc. Then, check if the parent of it has a humanoid and it is a real player.

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

local c = workspace.htisguy.Sound
local o
local cc = workspace.htisguy
local k = SoundService.Sound

cc.Touched:Connect(function(limb)
    local character = limb.Parent
    local humanoid = character:FIndFirstChildWhichIsA("Humanoid")
    local player = Players:GetPlayerFromCharacter(character)

    if character and humanoid and player then -- if it's a real player
        o = player

        k:Pause()
        c:Play()
        task.wait(0.5)
        o.PlayerGui.warp.Enabled = true
    end
end)

But if you want to play the sound ONLY to the player who touched it, you can make it a local script and put it in StarterGui or StarterPlayerScripts because local scripts won't work in workspace.

Ad
Log in to vote
1
Answered by 1 year ago

Theres no such thing as LocalPlayer on a Server-Sided script. Either find another way to reference players who fall into the pit or turn this into a LocalScript

0
I see. Thank you! EricAndTheWell 11 — 1y

Answer this question