Remote Event sending number value when shouldn't be?

My remote event is sending the target and printing numbers? Yet it should be the player’s character

Error: attempt to index number with ‘CFrame’

Client sided:

local function castRay(Target)
	
	print("Target in castRay is set to"..Target)
	
	local laser_Thickness = .5
	
	local rayOriginPart = Eye
	local rayTargetPart = Target
	
	
	
	local origin = rayOriginPart.CFrame.Position
	local rayTarget = rayTargetPart.CFrame.Position
	local direction = (rayTarget - origin)

	local results = workspace:Raycast(origin, direction, RayCastParameters)

	if not results then
		results = {Position = rayTarget}
	end

	local distance = (results.Position - origin).Magnitude
	
	LaserPart.Size = Vector3.new(
		laser_Thickness,
		laser_Thickness,
		distance
	)


	LaserPart.CFrame = CFrame.new(origin, rayTarget) * CFrame.new(0,0, -distance / 2)
	
	
	--game.ReplicatedStorage.EyeEvent:FireAllClients(origin, rayTarget, distance, LaserPart)
end

EyeEventRemote.onClientEvent:Connect(function(Target)
	castRay(Target)
end)

RunService.RenderStepped:Connect(castRay)

Server Side:

local EyeEventRemote = game:GetService("ReplicatedStorage"):WaitForChild("EyeEvent")

local Eye = game.Workspace.HappyHome.Home.EyeOfSauron.Eye
local Map = game:GetService("Workspace"):FindFirstChild("HappyHome"):Clone()

local Permitted = {28220859}

function FindNearest()
	local ChosenPlayer = nil
	local ChosenDistance = math.huge
	for _,Player in ipairs(game.Players:GetPlayers()) do
		local PrimaryPart = Player.Character and Player.Character.PrimaryPart
		local NewDistance = PrimaryPart and (Eye.Position - PrimaryPart.Position).Magnitude
		if PrimaryPart and ChosenDistance > NewDistance then
			ChosenDistance = NewDistance
			ChosenPlayer = Player
		end
	end
	return ChosenPlayer and ChosenPlayer.Character
end

game.Players.PlayerAdded:Connect(function(player)
	if table.find(Permitted, player.UserId) then
		player.Chatted:Connect(function(msg)
			if msg == "!EyeEvent" then

				if game.Workspace.HappyHome then
					MinigameActive = true
				else
					Map:Clone().Parent = workspace
					MinigameActive = true
				end
			elseif msg == "!EndEvent" then
				MinigameActive = false
			elseif msg == "!Reset" then
				workspace.HappyHome:Destroy()
				Map:Clone().Parent = workspace
			end
		end)
	end
end)


MinigameActive = false

function startGame()
	
	local Target = FindNearest()
	
	local TargetPrimaryPart = Target or Target.PrimaryPart
	
	
	EyeEventRemote:FireAllClients(Target)
	
	local Detector = Instance.new("Part")
	Detector.Anchored = true
	Detector.CanCollide = false
	Detector.Transparency = 0.5
	Detector.Color = Color3.fromRGB(255, 0, 0)
	Detector.Size = Vector3.new(1, 1, 1) * 4
	
end

while true do
	if MinigameActive == true then
		startGame()
	end
	wait(10)
end

While I’m not sure as to why it’s sending a number, You need to change this:

In the FindNearest() function, you return the target, then the character. So, it should be something like this:

local TargetPlayer, Target = FindNearest() --TargetPlayer will be the player itself and Target the character.

The problem is with this line, which doesn’t have a Target argument given. From the RunService | Roblox Creator Documentation wiki, RenderStepped has an argument deltaTime which is “The time (in seconds) that has elapsed since the previous frame”; that is, a number.

2 Likes