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

How to fix: Players.Popvlad2008.PlayerScripts.CompleteWorld:9: attempt to index nil with 'Parent'?

I've made a script where if you touch a brick it changes color, but only on the client's screen. I finally got it to work, but then there's this error in the title ^

The script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local completeWorldEvent = ReplicatedStorage:WaitForChild("CompleteWorldEvent") --RemoteEvent
 
local endPart1 = game.Workspace.World1.EndPart --1st part
local endPart2 = game.Workspace.World2.EndPart --2nd part


local function checkEndPart1(partTouching) --1st part script
	if partTouching.Parent:FindFirstChild("Humanoid") then --line that makes the error
		local player = game.Players:GetPlayerFromCharacter(partTouching.Parent)
		if endPart1.Color == Color3.fromRGB(255,255,0) then
			endPart1.Color = Color3.fromRGB(0,255,0)
		end	
	end
end

game.Workspace.World1.EndPart.Touched:Connect(checkEndPart1)


local function checkEndPart2(partTouching) --2nd part script
	if partTouching.Parent:FindFirstChild("Humanoid") then --similar line, also makes error
		local player = game.Players:GetPlayerFromCharacter(partTouching.Parent)
		if endPart2.Color == Color3.fromRGB(255,255,0) then
			endPart2.Color = Color3.fromRGB(0,255,0)
		end
	end
end

game.Workspace.World2.EndPart.Touched:Connect(checkEndPart2)


completeWorldEvent.OnClientEvent:Connect(checkEndPart1)
completeWorldEvent.OnClientEvent:Connect(checkEndPart2)

I'm a noob at scripting so I can't figure out the issue, can someone help please? <3

1 comment

0

I'll check back in like 2-3 hours

1 answer

✓ Accepted answer

Please look at the comments in function checkEndPart1

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local completeWorldEvent = ReplicatedStorage:WaitForChild("CompleteWorldEvent") --RemoteEvent

local endPart1 = game.Workspace.World1.EndPart --1st part
local endPart2 = game.Workspace.World2.EndPart --2nd part


local function checkEndPart1(partTouching) --1st part script
	if typeof(partTouching) ~= "Instance" then--check if not part is an object
		print("Part is not a valid part")
		return
	end
	if partTouching.Parent == nil then--if part's parent == nil then
		print("Parent is nil.")
		return
	end
    if partTouching.Parent:FindFirstChild("Humanoid") then --line that makes the error
        local player = game.Players:GetPlayerFromCharacter(partTouching.Parent)
        if endPart1.Color == Color3.fromRGB(255,255,0) then
            endPart1.Color = Color3.fromRGB(0,255,0)
        end 
    end
end

game.Workspace.World1.EndPart.Touched:Connect(checkEndPart1)


local function checkEndPart2(partTouching) --2nd part script
	if typeof(partTouching) ~= "Instance" then--check if not part is an object
		print("Part is not a valid part")
		return
	end
	if partTouching.Parent == nil then--if part's parent == nil then
		print("Parent is nil.")
		return
	end
    if partTouching.Parent:FindFirstChild("Humanoid") then --similar line, also makes error
        local player = game.Players:GetPlayerFromCharacter(partTouching.Parent)
        if endPart2.Color == Color3.fromRGB(255,255,0) then
            endPart2.Color = Color3.fromRGB(0,255,0)
        end
    end
end

game.Workspace.World2.EndPart.Touched:Connect(checkEndPart2)


completeWorldEvent.OnClientEvent:Connect(checkEndPart1)
completeWorldEvent.OnClientEvent:Connect(checkEndPart2)

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