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

Variable nil in an if that checks for it to be not nil?

I was coding a script that clones the player from the workspace and teleports it to a part in a "display" style, though it doesn't seem to work. There is an if statement that checks for the player in the workspace, then assigns a cloned version of the player and teleports it to the part in lines 26-31. I've included the code below along with the error message. The script is a local script. Workspace.Dave395PC.plrInit:28:attempt to index local 'plrClone' (a nil value)

wait(1)
plr = game.Players.LocalPlayer
Cam = game.Workspace.CurrentCamera
print(" || " .. plr.Name .. " has joined.")
local hum = plr.Character:WaitForChild("Humanoid", 4)
hum.MaxHealth = 100
hum.Health = hum.MaxHealth
plr.CameraMaxZoomDistance = 15

local intCent = script.MainMenu:Clone()
intCent.Parent = workspace
for _,child in pairs(intCent:GetChildren()) do
	if not child:IsA("Folder") then
		child.CanCollide = false
		child.Anchored = true
	end
end
script.Parent:FindFirstChild("CharRotate").Disabled = true
wait(5)
intCent.IntroCam.Position = Vector3.new(-270.125, -70, 80.75)
intCent.TorsoPos.Position = Vector3.new(-286.26, -73.982, 69.466)
intCent.CharBase.Position = Vector3.new(-286.25, -77.75, 69.375)
Cam.CameraType = Enum.CameraType.Scriptable
Cam.CFrame = intCent.IntroCam.CFrame

if workspace:WaitForChild(plr.Name, 10) then 
	local plrClone = workspace:FindFirstChild(plr.Name):Clone()
	plrClone.Parent = intCent.Player
	plrClone.CFrame = intCent.TorsoPos.CFrame
	local hum = plrClone:FindFirstChild("Humanoid") if hum:IsA("Humanoid") then hum:Destroy() end
end

2 comments

0

You're using WaitForChikd wrong. You should also be using more local variables. Always use them.

0

Just decided to assign the var with the WaitForChild and then check if the var was nil and now instead of throwing an error it does nothing. How am I using WaitForChild wrong?

1 answer

You're using WaitForChild incorrectly. Use FindFirstChild instead.

if workspace:FindFirstChild(plr.Name, 10) then

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