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
in line for why is the if statement wrapped in brackets ()?
four