Help with this error

Hello, so i just created a area purchasing system like bomb simulator everything is working fine but the output keeps on showing this error a lot of times here is the error:


Here is my script:

local door = script.Parent
script.Parent.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player:WaitForChild("Areas"):FindFirstChild(script.Parent.Name) then
			player.Character.Areas:FindFirstChild(script.Parent.Name).Disabled = false
		else
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid ~= nil then
		local pg = player:WaitForChild("PlayerGui")
		local frame = pg.Inventory.Frame
		local yes = frame.Yes
		frame:TweenPosition(UDim2.new(0.281, 0,0.519, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Elastic)
		frame.Text.Text = "Would you like to buy this area for ".. script.Parent.SurfaceGui.TextLabel.Text.."?"
		yes.MouseButton1Click:Connect(function()
			if player.leaderstats.Coins.Value >= script.Parent.Price.Value then
				player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - script.Parent.Price.Value
					player.Character.Areas:FindFirstChild(script.Parent.Name).Disabled = false
					local d = game.ReplicatedStorage.Areas:FindFirstChild(script.Parent.Name):Clone()
					d.Parent = player.Areas
					script.Disabled = true 
					wait(.2)
					script.Disabled = false
					yes.Parent:TweenPosition(UDim2.new(0.282, 0,1, 0), "Out", "Elastic")
				else
					frame.Text.TextColor3 = Color3.new(255, 0, 0)
					frame.Text.Text = "You don't have enough coins!"
					wait(1)
					frame.Text.TextColor3 = Color3.new(255, 255, 255)
					frame.Text.Text = "Would you like to buy this area for ".. script.Parent.SurfaceGui.TextLabel.Text.."?"
			end
			end)
			end
		end
end)

Thank you!

Seeing everything is coming from player I would add a check to make sure player isn’t nil.

if player ~= nil then
   --Code here
end
2 Likes

sometimes something might hit the part, that is not a player. When you call :GetPlayerFromCharacter(hit.Parent) it will either return the player, or will be nil

After you call it, check to see if it’s not nil before you continue:

local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
    -- rest of code
end
2 Likes

All of these are working thank you guys!