Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Attempt to index nil value?

I have a script that I want to do the following:

Player stands on a part. A ScreenGui called 'MapGui' is enabled, so that the player can view the Gui. A sound is then played, which is named 'Sound', and is a child to 'MapGui'. The player is then teleported to a random co-ordinate in a specific area.

However, when I attempt to test the script, I get the error 'Attempt to index a nil value', on the 'gui' variable in my script. Anyone know how I can fix this?

Code:

part = script.Parent

part.Touched:connect(function(hit)
	if (hit.Parent:FindFirstChild("Humanoid") ~= nil) then
		local player = game.Players:FindFirstChild(hit.Parent.Name)
		local gui = player.PlayerGui:FindFirstChild("MapGui")
		gui.Enabled = true
		gui:FindFirstChild("Sound"):Play()
		local xCor = math.random(-34, 0)
		local zCor = math.random(28.5,63.5)
		hit.Parent.Torso.CFrame = CFrame.new(xCor,1.5,zCor)
	end
end)

2 comments

0

in line for why is the if statement wrapped in brackets ()?

0

four

1 answer

This is not working because you used a Script to handle this. Use a LocalScript to make the Gui visible to the player. Put a Remote Event in ReplicatedStorage, and put this code in a LocalScript. As a bonus, this works for FE.

local show = Instance.new("RemoteEvent",game:GetService("ReplicatedStorage"))
show.Name = "ShowMap"

show.OnClientEvent:Connect(function(enabled)
    local pgui = game:GetService("Players").LocalPlayer:WaitForChild("PlayerGui")
    pgui:FindFirstChild("MapGui").Enabled = enabled
    pgui.MapGui.Sound:Play()
end)

Now, inside the Script, (Not local) do your code, and please do not use :connect it is deprecated, use :Connect. Using deprecated code can cause problems.

local part = script.Parent

part.Touched:Connect(function(part) -- Don't use :connect use :Connect
    if part.Parent:FindFirstChild"Humanoid" then -- Don't wrap your if statements in ()
    local plr = game:GetService("Players"):GetPlayerFromCharacter(part.Parent)
    if plr then
    
    local show = game:GetService("ReplicatedStorage"):WaitForChild("ShowMap")
    show:FireClient(plr, true) -- Specify the player first, then the arguments. I had a parameter, here is the argument.
end)

9 comments

0

There is an error on line 6 in the server script, on the if statement.

0

how?

0

^ There’s multiple errors on your second script. You might want to recheck it...

0

It should work now.

0

I completely forgot to fill in the line5, it should work now.

0

You forgot to include 'end' on your if statements

0

Works completely fine in Studio, however when I go in game, I get a server error in the development console stating 'Infinite yield possible on ReplicatedStorage:WaitForChild("ShowMap")', from line 8 of the server script.

0

Ah, just found out it's something to do with FE. I turned FE off, and it worked completely fine in-game. Any idea how I could get it to work with FE enabled?

0

All right, I got the WaitForChild part to work, but now I am getting the error 'Attempt to index local 'show' (a nil value)' from line 9 of the server script. Can you give any help?

This discussion is preserved as it was. New answers, comments, and votes are closed.