RaycastResult.Position is nil?

I am trying to make a script that places a part (The part’s name is particle) at the end of a raycast aka the RaycastResult’s Posititon (the RaycastResult’s name is raycast), but for some reason I get the error message “attempt to index nil with ‘Position’” which makes no sense, because RaycastResult Can’t be nil, as the “WallHit” event only fires when the raycast hits a wall. I have no idea why this happens.

script:

script.Parent.WallHit.OnServerEvent:Connect(function(Player, parthit, raycast)
	local particle = game.ServerStorage.Debris.WallParticlePart:Clone()
	local Particles = particle:GetChildren()
	for i, part in pairs(Particles) do
		if part.ClassName == "Script" then
		else
			part.Color = ColorSequence.new{
				ColorSequenceKeypoint.new(0, parthit.Color),
				ColorSequenceKeypoint.new(1, parthit.Color)
			}
		end
	end
	particle.Parent = workspace
	particle.Position = raycast.Position -- <= where the error happens --
end)

This could be because the raycast does not hit anything at all.

1 Like

As I have already stated, the event does not fire if the raycast does not hit anything.

script.Parent.WallHit.OnServerEvent:Connect(function(Player, parthit, raycast)
    if raycast and raycast.Position then
        local particle = game.ServerStorage.Debris.WallParticlePart:Clone()
        local Particles = particle:GetChildren()
        for i, part in pairs(Particles) do
            if part.ClassName == "Script" then
            else
                part.Color = ColorSequence.new{
                    ColorSequenceKeypoint.new(0, parthit.Color),
                    ColorSequenceKeypoint.new(1, parthit.Color)
                }
            end
        end
        particle.Parent = workspace
        particle.Position = raycast.Position
    end
end)

this could help

1 Like

Before, the part would appear, but only in the center of the map, but now it does not appear at all, which means that raycast is nil? But I have used raycast.Position in another script (the script where the raycast originates from) and it works just fine, but in this script it does not work. Maybe the RemoteEvent messes something up?

Can you post your function firing the RemoteEvent?

Remember that you don’t need to specify the player;

RemoteEvent:FireServer(parthit, raycast)
as opposed to:
RemoteEvent:FireServer(player, parthit, raycast)

Also remember that FireServer can’t replicate things to the server, so you can only send details/properties about things to the server such as raycast.Position rather than the instance ‘raycast’

Hope this helps :slight_smile:

1 Like

I changed the code that fires the script to

script.Parent.WallHit:FireServer(parthit, raycast.Position)

and the one that puts the part at the end of the raycast to

script.Parent.WallHit.OnServerEvent:Connect(function(Player, parthit, raycast)
	local particle = game.ServerStorage.Debris.WallParticlePart:Clone()
	local Particles = particle:GetChildren()
	for i, part in pairs(Particles) do
		if part.ClassName == "Script" then
		else
			part.Color = ColorSequence.new{
				ColorSequenceKeypoint.new(0, parthit.Color),
				ColorSequenceKeypoint.new(1, parthit.Color)
			}
		end
	end
	particle.Parent = workspace
	particle.Position = raycast
end)

And it works now. Thanks!

1 Like

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