Hi there! I’m trying to make a script that when a part is touched, a gui pops up for a player, and only that player. I’ve attempted to use PlayerGui to accomplish this, but to no avail.
My code:
local part = game.Workspace.Lobby.ThePyramidsPortal
part.Touched:Connect(function(otherPart)
local character = otherPart.Parent
local players = game:GetService("Players")
local realPlayer = players:FindFirstChild(character)
local PlayerGui = game:GetService('Players').LocalPlayer:WaitForChild('PlayerGui')
local frame = realPlayer.PlayerGui.ThePyramidsGUI.Frame
frame.Visible = true
end)
The error: [ Players.TheEternalGreat.PlayerGui.ThePyramidsGUI.LocalScript:10: attempt to index nil with ‘PlayerGui’]
What I would do, is to first call the Player, then the PlayerGui, like this:
local Player = game.Players.LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")
Then continue on with your script, like this:
local part = game.Workspace.Lobby.ThePyramidsPortal
local PlayerService = game:GetService("Players")
local Player = PlayerService.LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")
part.Touched:Connect(function(otherPart)
local Character = otherPart.Parent
local realPlayer = Player:WaitForChild(Character)
local frame = PlayerGui.ThePyramidsGUI.Frame
frame.Visible = true
end)
Make sure it is in a LocalScript, or else it might not work!
I have not tested the script, so let me know if you encounter any problems. ^
Put this LocalScript in StarterPlayer in StarterCharacterScripts
> -- LocalScript
>
> local Player = game.Players.LocalPlayer
> local PlayerGui = Player:WaitForChild("PlayerGui")
> local ScreenGui = PlayerGui:WaitForChild("ScreenGui")
> local TextLabel = ScreenGui:WaitForChild("TextLabel")
> TextLabel.Visible = false
>
> local model = script.Parent
>
> local function onModelTouched(part)
>
> if part:IsDescendantOf(model) then return end
> print(part.Name)
>
> if part.Name == "First" or part.Name == "Second" or part.Name == "Third" or part.Name == "Fourth" then
> TextLabel.Visible = true
> TextLabel.Text = part.Name
> else
> TextLabel.Visible = false
> TextLabel.Text = ""
> end
>
> end
>
> for _, child in pairs(model:GetChildren()) do
> if child:IsA("BasePart") then
> child.Touched:Connect(onModelTouched)
> end
> end
Just add a script into the part itself and add script.Parent.Touched:connect(function (hit)
If hit.Parent:FindFirstChild(“Humanoid”) then
local player = hit.Parent
player.PlayerGui.ThePyramidsGUI.Frame.Visible = true
end)
You might want to have a script in the part, remote event in Replicated storage, and a local script in the gui frame. So that you can juts fire the client and make the gui visible. (Only use Remot Event if your gui is not that important.
When I say not that important. It includes shops and notices. Important things are like if the gui gives you in game currency and so)