Why can't my Local Script find a part in my workspace?

Idea

I am trying to make an obby, where you have to complete a series of parkour stuff.
And you have to touch an obby so my text GUI will say something

Problem

The problem is that it can’t find my part.

Here is the script

local Timer = script.Parent
local timevalue = 20
local part = game.Workspace.Complete

local function TimerStart()
	for i = 1, 20 do
		wait(1)
		timevalue = timevalue - 1

		Timer.Text = timevalue
	end
	Timer.Text = "You have failed to complete this trial."
end

local function onTouched(part)
	local humanoid = part.Parent:FindFirstChild("Humanoid")
	if humanoid and timevalue > 0 then
		Timer.Text = "You have completed the obby!"
		timevalue = 0
	end
end

while wait(1) do
	TimerStart()
end

script.Parent.Touched:Connect(onTouched)

Hierarchy

image

image

1 Like

It’s because you put your while true do loop before your event, therefore the function is never connected to it because it’s waiting for the loop to end.

Fixed code:

--//Services
local Players = game:GetService("Players")

--//Variables
local LocalPlayer = Players.LocalPlayer
local Timer = script.Parent
local completePart = workspace.Complete

--//Controls
local timeValue = 20
local hasFinished = false

--//Functions
local function TimerStart()
	hasFinished = false
	timeValue = 20
	
	while timeValue >= 0 and not hasFinished do
		task.wait(1)
		
		timeValue -= 1
		Timer.Text = timeValue
	end
	
	if hasFinished then
		Timer.Text = "You have completed the obby!"
	else
		Timer.Text = "You have failed to complete this trial."
	end
end

local function onTouched(part)
	local player = Players:GetPlayerFromCharacter(part.Parent)
	
	if not player or player ~= LocalPlayer or timeValue <= 0 then
		return
	end
		
	hasFinished = true
	timeValue = 0
end

completePart.Touched:Connect(onTouched)

while task.wait(1) do
	TimerStart()
end

I also made added a check in the .Touched function so that it will only say “You have completed the obby” if you were the one to complete it. (and not if someone else completes it)

So it will only be able to find the part, once my loop is done?

Yes, but that loop is never finished, therefore it is never found. You can use my fixed code.

It still didn’t work, I used your code.

It was on the same line you declared the variable.

image

Try this, Complete probably didn’t load in time:

--//Services
local Players = game:GetService("Players")

--//Variables
local LocalPlayer = Players.LocalPlayer
local Timer = script.Parent
local completePart = workspace:WaitForChild("Complete")

--//Controls
local timeValue = 20
local hasFinished = false

--//Functions
local function TimerStart()
	hasFinished = false
	timeValue = 20

	while timeValue >= 0 and not hasFinished do
		task.wait(1)

		timeValue -= 1
		Timer.Text = timeValue
	end

	if hasFinished then
		Timer.Text = "You have completed the obby!"
	else
		Timer.Text = "You have failed to complete this trial."
	end
end

local function onTouched(part)
	local player = Players:GetPlayerFromCharacter(part.Parent)

	if not player or player ~= LocalPlayer or timeValue <= 0 then
		return
	end

	hasFinished = true
	timeValue = 0
end

completePart.Touched:Connect(onTouched)

while task.wait(1) do
	TimerStart()
end
4 Likes

Thanks, it worked!

The script probably ran before the part loaded.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.