Weird Error that I'm getting

Hello fe!!ow forum members,

I’m making a SSB type game cald Brickbattle: Grand Arena or just B:GA/BGA for short. I want players to be able to jump from under a part when a raycast is touched, and then I would give the player more assembly velocity on the Y axis so that the player can go up, I would then check if there is a positional difference between the raycast’s target (floatingPlatform) and the raycast, and if their was, I would make the platform’s collision true again.

I’m getting this error though, and I’m not sure how to fix it, can anyone help?

  21:18:45.864  Workspace.NubblyFry.LocalScript:35: attempt to index nil with 'Position'  -  Client - LocalScript:35
  21:18:45.864  Stack Begin  -  Studio
  21:18:45.864  Script 'Workspace.NubblyFry.LocalScript', Line 35  -  Studio - LocalScript:35
  21:18:45.864  Stack End  -  Studio
local char = script.Parent
local hrp = char:WaitForChild("HumanoidRootPart")
local head = char:WaitForChild("Head")
local rayOrigion = head.Position
local rayDirection = Vector3.new(0, 2, 0)

local raycastResult = workspace:Raycast(rayOrigion, rayDirection)

local raycastParams = RaycastParams.new()
raycastParams.IgnoreWater = true
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {char}

local part = Instance.new("Part", char)
part.Anchored = true
part.Position = rayOrigion
part.Size = Vector3.new(1, 1, 1)
part.Material = Enum.Material.Neon
part.BrickColor = BrickColor.new("Persimmon")
part.CanCollide = false
part.Name = ("PlatformJumpRaycast")

part.Position = head.Position + Vector3.new(0, 1, 0)

game:GetService("RunService").Heartbeat:Connect(function()
	raycastResult = workspace:Raycast(rayOrigion, rayDirection, raycastParams)
end)

game:GetService("RunService").Heartbeat:Connect(function()
	rayOrigion = head.Position part.Position = rayOrigion
	if raycastResult then
		part.Size = Vector3.new(1, (rayOrigion - raycastResult.Position), 1)
		if raycastResult.Instance.Name == "floatingPlatform" then
			raycastResult.Instance.CanCollide = false
			while (hrp.Position - raycastResult.Position).Magnitude < 7 and raycastResult do task.wait(.025)
				hrp.AssemblyLinearVelocity += Vector3.new(0, 25, 0)
			end
			raycastResult.Instance.CanCollide = true
		end 
	end
end)

I believe this is line 35:

while (hrp.Position - raycastResult.Position).Magnitude < 7 and raycastResult do task.wait(.025)

Maybe try checking if hrp isn’t nil, and also, maybe reformat, so that if raycastResult is before check it’s position, like this:

while hrp and raycastResult and (hrp.Position - raycastResult.Position).Magnitude < 7 do task.wait(.025)
1 Like

hrp is always active, this is evident as the players character always has it in their.

My assumption is that its related to the raycast not intersecting with the floatingPlatform after the players character’s Y axis increases.

Yeah, I think this would make the raycastResult nil, my question is, why do you have two Heartbeat event listeners, this is the source of the problem, becasue during the second loop, that needs the raycastResult, the first one may be changing it (to either the result or nil).

This is also why checking the raycastResult earlier doesn’t work 100% (I think).

It doesn’t matter, if I combined the two, I would still be getting the same error as the raycast will eventually stop intersecting the floatingPlatform part, so this would be a bit more pointless, but it may save more memory and recourses on the client’s device.

The source of the problem has to be the fact that the raycast stops hitting the platform. This is evident, as when I put

while raycastResult ~= nil and (hrp.Position - raycastResult.Position).Magnitude < 7 do task.wait(.025)
	hrp.AssemblyLinearVelocity += Vector3.new(0, 25, 0)
end

It actually ended up going to the next line of code, and convienently gave me this error.

 21:48:54.225  Workspace.NubblyFry.LocalScript:38: attempt to index nil with 'Instance'  -  Client - LocalScript:38
  21:48:54.225  Stack Begin  -  Studio
  21:48:54.225  Script 'Workspace.NubblyFry.LocalScript', Line 38  -  Studio - LocalScript:38
  21:48:54.225  Stack End  -  Studio

This is why you use if raycastResult then, but I think it didn’t work in the previous code because they weren’t in sync.

Edit: why are you looping in the Heartbeat function, and do you absolutely need to? (also use local variables)

I don’t have a general understanding of RunService functions.

what local variables? I’m not sure what your talking about, everything is local?

Solution was to use a for loop and quickly get the player up onto the platform, then re-enable its collision.

local char = script.Parent
local hrp = char:WaitForChild("HumanoidRootPart")
local head = char:WaitForChild("Head")
local rayOrigion = head.Position
local rayDirection = Vector3.new(0, 2, 0)

local raycastResult = workspace:Raycast(rayOrigion, rayDirection)

local raycastParams = RaycastParams.new()
raycastParams.IgnoreWater = true
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {char}

local part = Instance.new("Part", char)
part.Anchored = true
part.Position = rayOrigion
part.Size = Vector3.new(1, 1, 1)
part.Material = Enum.Material.Neon
part.BrickColor = BrickColor.new("Persimmon")
part.CanCollide = false
part.Name = ("PlatformJumpRaycast")

part.Position = head.Position + Vector3.new(0, 1, 0)

game:GetService("RunService").Heartbeat:Connect(function()
	raycastResult = workspace:Raycast(rayOrigion, rayDirection, raycastParams)
	rayOrigion = head.Position part.Position = rayOrigion
	if raycastResult then
		part.Size = Vector3.new(1, (rayOrigion - raycastResult.Position), 1)
		if raycastResult.Instance.Name == "floatingPlatform" then
			local lastRayPart = raycastResult.Instance
			raycastResult.Instance.CanCollide = false
			for i = 1,2, 2 do
				hrp.AssemblyLinearVelocity += Vector3.new(0, 7.5, 0)
				task.wait(.4)
			end
			lastRayPart.CanCollide = true
		end 
	end
end)

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