Why am I Getting this Error?

Im getting a error for my Seat.

local sitAnim = 18201895983
local seat = script.Parent
local playingAnim

local player = game:GetService("Players"):FindFirstChildOfClass("Player")

local newAnim = Instance.new('Animation')
newAnim.AnimationId = 'rbxassetid://'..sitAnim

seat:GetPropertyChangedSignal("Occupant"):Connect(function(Occupant)
	local Player = game.Players:GetPlayerFromCharacter(Occupant.Parent)
	local Character = Occupant.Parent
	local rightHand = Character.RightHand
	
	if Occupant and Occupant:IsA("Humanoid") and not playingAnim then
		playingAnim = Occupant:LoadAnimation(newAnim)
		playingAnim:Play()

	elseif Occupant and Occupant:IsA("Humanoid") and playingAnim then
		local model = rightHand:WaitForChild("BasicBenchPressWeights", 5)
		local motor6D = rightHand:WaitForChild("BenchpressMotor6D", 5)
		
		model:Destroy()
		motor6D:Destroy()
		playingAnim:Stop()
		playingAnim = nil
	end
end)    

Error:

2 Likes

the seat does not exist at the time the script runs
since you can’t waitforchild a parent (a parent is not a child,) you need to use the wait()

where should i put the wait()?

before the script.parent line (adjust values based on how long the thing takes to load. if the seat is far from spawn, probably wait longer just to be safe)

Occupant is not a valid variable you get from the :GetPropertyChangedSignal() function. Use seat.Occupant as a replacement when fetching the player.

local Player = game.Players:GetPlayerFromCharacter(seat.Occupant.Parent)

it still outputs the same error

have you tried the wait yet? because the error message basically say the script not exist

the only place i see script.Parent is the variable seat, is that what you meant?

The problem of his error is the value, because the value of the “Parent” is nil. (I think)

vvhats the correct vvay of getting the value of the seat occupant?

Does this error only happen when you leave the seat? Also, :GetPropertyChangedSignal doesn’t return any value so you would need to do seat.Occupant

it errors on enter and exit 30303030

Maybe try adding this to the top of the function

if not seat.Occupant then return end

it no longer errors on that line, but it errors on the local character line

Can you post the current version of your script?

local sitAnim = 18201895983
local seat = script.Parent
local playingAnim

local player = game:GetService("Players"):FindFirstChildOfClass("Player")

local newAnim = Instance.new('Animation')
newAnim.AnimationId = 'rbxassetid://'..sitAnim

seat:GetPropertyChangedSignal("Occupant"):Connect(function(Occupant)
	if not seat.Occupant then return end
	local Player = game.Players:GetPlayerFromCharacter(seat.Occupant.Parent)
	local Character = Occupant.Parent
	local rightHand = Character.RightHand
	
	if Occupant and Occupant:IsA("Humanoid") and not playingAnim then
		playingAnim = Occupant:LoadAnimation(newAnim)
		playingAnim:Play()

	elseif Occupant and Occupant:IsA("Humanoid") and playingAnim then
		local model = rightHand:WaitForChild("BasicBenchPressWeights", 5)
		local motor6D = rightHand:WaitForChild("BenchpressMotor6D", 5)
		
		model:Destroy()
		motor6D:Destroy()
		playingAnim:Stop()
		playingAnim = nil
	end
end)    

You have to replace Occupant with seat.Occupant or create a variable for it

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
    local Occupant = seat.Occupant
	if not Occupant then return end
	local Player = game.Players:GetPlayerFromCharacter(Occupant.Parent)
	local Character = Occupant.Parent
	local rightHand = Character.RightHand
	
	if Occupant and Occupant:IsA("Humanoid") and not playingAnim then
		playingAnim = Occupant:LoadAnimation(newAnim)
		playingAnim:Play()

	elseif Occupant and Occupant:IsA("Humanoid") and playingAnim then
		local model = rightHand:WaitForChild("BasicBenchPressWeights", 5)
		local motor6D = rightHand:WaitForChild("BenchpressMotor6D", 5)
		
		model:Destroy()
		motor6D:Destroy()
		playingAnim:Stop()
		playingAnim = nil
	end
end)

novv theres no errors, but the model and motor6d arent destroying, is there a better vvay to do it?

Can you print the model and motor6D to see if they are nil? By the way, you may want to use FindFirstChild instead of WaitForChild.

alr let me check in 5 minutes 303030

1 Like