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)
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