Attempt to index nil with "isRiding"

  1. I want that part of the script in the waterbike to get the player’s ‘isRiding’ value which determines whether he is currently riding on a vehicle or not.

  2. it works at the beginning of the game and works when I delete and put new bikes for the new round. After a couple of rounds, it gives me the error

Workspace.waterbike.Script:18: attempt to index nil with ‘isRiding’


  1. I have tried putting waitforchild but it gives an infinite yield.

here is one of the waterbike scripts. There is a lot of water bikes in the game and after 1 round of racing with them, I delete and respawn new waterbikes.

local anim2 = script.Parent.fanANimation
local Humanoid = script.Parent.Humanoid
local animator = Humanoid.Animator
local hmr = script.Parent.HumanoidRootPart
local anim = animator:LoadAnimation(anim2)
local ridingAnim = script.Parent.ridingAnimation
local touchAllowed = Instance.new("BoolValue")
touchAllowed.Parent = script.Parent
local remote = game.ReplicatedStorage.remotes.bikeRemote
touchAllowed.Value = true
hmr.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if touchAllowed.Value == true then
	if humanoid then
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		if plr then
			print(plr)
				isRiding = plr.isRiding
			canTouchFinishLine = plr.canTouchFinishLine
			anim:Play()
			
				isRiding.Value = "waterBike"
				touchAllowed.Value = false
				local humr = hit.Parent:FindFirstChild("HumanoidRootPart")
				humr.CFrame = script.Parent.weldingPos.CFrame
				local weld = Instance.new("Weld")
				weld.Parent = script.Parent.sit
				weld.Part0 = humr
				weld.Part1 = script.Parent.weldingPos
				ridingonahorse = humanoid:LoadAnimation(ridingAnim)
				ridingonahorse:Play()
			end
		end
	end
end)

	
remote.OnServerEvent:Connect(function()
	isRiding.Value = "false"
	canTouchFinishLine.Value = true
	game.Workspace.waterBike.sit.Weld:Destroy()
	ridingonahorse:Stop()
	wait(2)
	touchAllowed.Value = true
	print("put stuff false for")
end)
1 Like

By the looks of it, isRiding is not a child of player.

It is tho. I instance it with a sript.

Make an isRiding variable with the value nil at the very top and try that.
Might fix it because isRiding was never actually defined as a variable and you wouldn’t be able to get it for the remote event if it was inside that touch function.

1 Like

as you can see it was defined as a variable. Do you mean that you want me to define it as a variable earlier? like maybe before the function? If so I don’t think that would change much since the I think that the script runs and checks the variables directly. Correct me if I am wrong.