Player GUI issue

I need help ASAP. I seem to have a bug with the player UI.

Script:

local part = script.parent
local ProximityPrompt = part.ProximityPrompt
local workspace = game.Workspace
local UI = game.Players.LocalPlayer.PlayerGui.ScreenGui.Candy

ProximityPrompt.Triggered:connect(function()
	if UI.Text == "1st. Find all the candy (0/20)" then
		UI.Text = "1st. Find all the candy (1/20)"
	elseif UI.Text == "1st. Find all the candy (1/20)" then
		UI.Text = "1st. Find all the candy (2/20)"
	elseif UI.Text == "1st. Find all the candy (2/20)" then
		UI.Text = "1st. Find all the candy (3/20)"
	elseif UI.Text == "1st. Find all the candy (3/20)" then
		UI.Text = "1st. Find all the candy (4/20)"
	elseif UI.Text == "1st. Find all the candy (4/20)" then
		UI.Text = "1st. Find all the candy (5/20)"
	elseif UI.Text == "1st. Find all the candy (5/20)" then
		UI.Text = "1st. Find all the candy (6/20)"
	elseif UI.Text == "1st. Find all the candy (6/20)" then
		UI.Text = "1st. Find all the candy (7/20)"
	elseif UI.Text == "1st. Find all the candy (7/20)" then
		UI.Text = "1st. Find all the candy (8/20)"
	elseif UI.Text == "1st. Find all the candy (8/20)" then
		UI.Text = "1st. Find all the candy (9/20)"
	elseif UI.Text == "1st. Find all the candy (9/20)" then
		UI.Text = "1st. Find all the candy (10/20)"
	elseif UI.Text == "1st. Find all the candy (10/20)" then
		UI.Text = "1st. Find all the candy (11/20)"
	elseif UI.Text == "1st. Find all the candy (11/20)" then
		UI.Text = "1st. Find all the candy (12/20)"
	elseif UI.Text == "1st. Find all the candy (12/20)" then
		UI.Text = "1st. Find all the candy (13/20)"
	elseif UI.Text == "1st. Find all the candy (13/20)" then
		UI.Text = "1st. Find all the candy (14/20)"
	elseif UI.Text == "1st. Find all the candy (14/20)" then
		UI.Text = "1st. Find all the candy (15/20)"
	elseif UI.Text == "1st. Find all the candy (15/20)" then
		UI.Text = "1st. Find all the candy (16/20)"
	elseif UI.Text == "1st. Find all the candy (16/20)" then
		UI.Text = "1st. Find all the candy (17/20)"
	elseif UI.Text == "1st. Find all the candy (17/20)" then
		UI.Text = "1st. Find all the candy (18/20)"
	elseif UI.Text == "1st. Find all the candy (18/20)" then
		UI.Text = "1st. Find all the candy (19/20)"
	elseif UI.Text == "1st. Find all the candy (19/20)" then
		UI.Text = "1st. Find all the candy (20/20)"
	end
	script.Parent:Destroy()
	script.Sound:Play()
	wait(1)
	script.Parent:Remove()
end)

Error Message:
Attempted to index nil with ‘PlayerGui’ (Line 4).

Anyone know how to fix this?

Try:

local UI = game.Players.LocalPlayer:WaitForChild("PlayerGui").ScreenGui.Candy
1 Like

Still the same issue. I don’t understand why it wont work when everything is done right

You can’t get the LocalPlayer with server script, instead when prompt is triggered you get which player has triggered it. Also you should not check what their text says, just create a Candy value for every player when they join the game and change that when prompt is triggered.

--put this in some other script
game.Players.PlayerAdded:Connect(function(player)
    local candy = Instance.new("IntValue")
    candy.Name = "Candy"
    candy.Parent = player
end)

--

local part = script.parent
local ProximityPrompt = part.ProximityPrompt
local workspace = game.Workspace

ProximityPrompt.Triggered:Connect(function(player)
    local UI = player.PlayerGui.ScreenGui.Candy
    local candyamt = player.Candy
    candyamt.Value = math.clamp(candyamt.Value+1,1,20)
	UI.Text = "1st. Find all the candy ("..candyamt.Value.."/20)"
	
	script.Parent:Destroy()
	script.Sound:Play()
	wait(1)
	script.Parent:Remove()
end)
1 Like