Help with Raycasting Server Script Error

Hi, I’m making a pushing system for my game. It’s all going well, however I’m struggling with one issue that appears near the end of the server side script. Note this is my first time using Raycasting and I am still new to this.

  1. What is the issue?
    On the server-side script at Line 16 there is an error stating “Instance is not a valid member of Player Players.[PlayerUsername]” (note the playerusername is supposed to be filled in with your actual user)

  2. What solutions have you tried so far?
    I looked on similar DevForums and the creator hub for help.

Part of the Server Script

local function OnRayHit(raycastResult: RaycastResult)
	if raycastResult then
		if tool:GetAttribute("CanPush") == true then
			if raycastResult.Instance.Parent:FindFirstChild("Humanoid") then
				local character = raycastResult.Instance.Parent
				
				character.Humanoid:TakeDamage(100)
			end
		end
	end
end

remoteEvent.OnServerEvent:Connect(function(raycastResult: RaycastResult)
	OnRayHit(raycastResult)
end)

Part of the Client Script

			if inputType == Enum.UserInputType.Touch or inputType == Enum.UserInputType.MouseButton1 then
				print("Raycast")
				local raycastResult = rayResult(input.Position.X, input.Position.Y)
				
				if raycastResult.Instance.Parent:FindFirstChild("Humanoid") then
					push:FireServer(raycastResult)
					print("Hit player")
				end
			end
		end
	end
end)

Are you sure line 16 is within the code snips given to us?

Yes. To be more specific, line 16 is the server script part that says “if raycastResult.Instance.Parent:FindFirstChild(“Humanoid”) then”

Oh check if the ray-cast instance doesn’t equal nil.

I did some printing and the raycast result comes up with my name before an error happens. Just saying that I clicked the dummy rig and not myself

OH seems like you inputed the player name or whatever it is into the function and not the raycast result

Can I know which function you mean exactly?

OnRayHit perhaps? CHARACTER LIMIT

The first argument of a remote event is the player who fired it, this should actually be

remoteEvent.OnServerEvent:Connect(function(player:Player,raycastResult: RaycastResult)
	OnRayHit(raycastResult)
end)
2 Likes

Strangely, I tried this and the tool still doesn’t work. But there was an error, similar to the one i mentioned above:

“Players.[PlayerName].Backpack.Push.Server:17: attempt to index nil with ‘Instance’
Stack Begin
Script ‘Workspace.[PlayerName].Push.Server’, Line 17 - function onRayHit
Script ‘Workspace.[PlayerName].Push.Server’, Line 33
Stack End”

The error is different this time, give us the snips of codes for both locations

Of course, sorry!

Here you go

local function OnRayHit(raycastResult)
	print("First hphase")
	if tool:GetAttribute("CanPush") == true then
		print("Tool is equipped")
		if raycastResult.Instance.Parent:FindFirstChild("Humanoid") then
			print("Found humanoid")
			local character = raycastResult.Instance.Parent
				
			character.Humanoid:TakeDamage(100)
			
			print("Took damage")
		end
	end
end


remoteEvent.OnServerEvent:Connect(function(player:Player,raycastResult: RaycastResult)
	OnRayHit(raycastResult)
end)

Your doing the wrong raycast method.
try this instead: local raycastResult = game.Workspace:RayCast(origin, direction)
The origin is where you want the ray to start and the direction is the direction that you want the ray to go. To get the direction between two vectors do something like this → (part2.Position - part1.Position).unit which is just subtracting the two vectors and turning the result into a direction

1 Like

Can we see the rayResult() function’s code in the client script? The data you’re sending to the server is always nil for sone reason, so it has to be an issue within that function. I would also add a print(typeof(raycastResult)) just to check if it really is nil right inside the OnRayHit() function.

Here, this should be enough to get you through I hope. If there’s any problems with this please tell me.

local function rayResult(x, y)
	local unitRay = camera:ScreenPointToRay(x, y)
    return workspace:Raycast(unitRay.Origin, unitRay.Direction * 10000, params)
end

Hm, maybe it’s an issue with your raycastParams? You could try setting that to nil and see if that fixes anything. I’d also recommend trying to ray set from the humanoidRootPart’s look vector, something like

local hrp = game.Players.LocalPlayer.Character.HumanoidRootPart 
return workspace:Raycast(hrp.Position,hrp.CFrame.LookVector*10000,params--[[or nil since i also think this could be a problem]])

Alright, I tried that. Luckily, the raycast identifies the player rig and fires the remote event. But the passed raycast result is nil when it’s being used on the server side script.