Unable to cast value to Object

I’m working on a tool that shoots a neon ball to where the player’s mouse is on the screen, however I couldn’t really get it work. It used to work completely fine when I was writing the script in ONLY a LocalScript, but once I added a Server script to the tool and added client to server communication It’s been bugging out. The only problem I’ve been facing for the past hour is getting the Hit.Position property of the players mouse. I’ve tried like Every possible solution for this but it just doesn’t want to work.

Here’s the server script:

repeat
	task.wait()
until workspace["Alive"] ~= nil

local player2 : Player
local char : Model

local events = {}

for _, z in pairs(script.Parent:GetDescendants()) do
	if z:IsA("RemoteEvent") or z:IsA("RemoteFunction") then
		events[z.Name] = z
	end
end

if script.Parent.Parent.Parent:IsA("Player") then
	player2 = script.Parent.Parent.Parent
	char = player2.Character or player2.CharacterAdded:Wait()
elseif script.Parent.Parent.Parent.Parent:IsA("Model") and script.Parent.Parent.Parent.Parent:FindFirstChildOfClass("Humanoid") then
	local m =script.Parent.Parent.Parent.Parent
	player2 = game:GetService("Players"):GetPlayerFromCharacter(m)	
end

local data = events["Data"]:InvokeClient(player2, {
	["Request"] = "PlayerStuff" -- Just returns a table with the Player's "Player" object and the Player's character.
})

local player

char = data.Character
player2 = data.Player
player = data.Player

local limbs = {}
local sounds = {}

for _, z in pairs(script.Parent:GetDescendants()) do
	if z:IsA("Sound") then
		sounds[z.Name] = z
	end
end

for _, z in pairs(char:GetChildren()) do
	if z:IsA("BasePart") then
		limbs[z.Name] = z
	end
end

local states = {
	Charged = false,
	Active = false
}

local cooldowns = {
	Shoot = false
}

local Moveset = {
	["Click"] = function(args)
		local BallFolder = workspace:FindFirstChild("Balls") or workspace:WaitForChild("Balls", 25)
		local PlayerBulletsContainer
		
		if not BallFolder:FindFirstChild(player.Name.."'s Container") then
			PlayerBulletsContainer = Instance.new("Folder")
			PlayerBulletsContainer.Name = player.Name.."'s Container"
			PlayerBulletsContainer.Parent = BallFolder
		else
			PlayerBulletsContainer = BallFolder:FindFirstChild(player.Name.."'s Container")
		end
		
		local ball = Instance.new("Part")
		ball.Shape = Enum.PartType.Ball
		ball.Size = Vector3.new(1, 1, 1)
		ball.BrickColor = states.Charged and BrickColor.new("Bright red") or BrickColor.new("Institutional white")
		ball.Material = Enum.Material.Neon
		ball.CFrame = limbs["HumanoidRootPart"].CFrame
		
		ball.Velocity = (events["Data"]:InvokeClient({Request = "MousePosition"}) - limbs["HumanoidRootPart"].Position).Unit * 500 
                -- The line above throws out the "Unable to cast value to Object" error btw

		ball.CanCollide = true
		ball.Parent = PlayerBulletsContainer
	end,
}

And here’s the client script that sends over information to the aforementioned Server script.

local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

local events = {}

for _, z in pairs(script.Parent:GetDescendants()) do
	if z:IsA("RemoteFunction") or z:IsA("RemoteEvent") then
		events[z.Name] = z
	end
end

events["Data"].OnClientInvoke = function(args)
	if args["Request"] == "PlayerStuff" then
		return {
			Player = player,
			Character = char
		}
		
	elseif args["Request"] == "Mouse" then
		return player:GetMouse().Hit.Position
	end
end

local uis = game:GetService("UserInputService")

local mouse

repeat
	task.wait()
	mouse = player:GetMouse()
until mouse ~= nil

print("Mouse loaded")

uis.InputBegan:Connect(function(i, g)
	if g then return end
	
	if i.UserInputType == Enum.UserInputType.MouseButton1 then
		events["Input"]:FireServer({
			Input = "Click",
			args = {
				MousePosition = mouse.Hit.Position
			}
		})
	end

Also, just for good measure or something, here’s a picture of the tool itself in the Explore page

image

Please help me out bru :sob::sob:

This is the line that throws out the error btw:

ball.Velocity = (events["Data"]:InvokeClient({Request = "MousePosition"}) - limbs["HumanoidRootPart"].Position).Unit * 500 
2 Likes

Replace ball.Velocity = (events["Data"]:InvokeClient({Request = "MousePosition"}) - limbs["HumanoidRootPart"].Position).Unit * 500 with ball.Velocity = (events["Data"]:InvokeClient(player, {Request = "Mouse"}) - limbs["HumanoidRootPart"].Position).Unit * 500 .
also elseif args["Request"] == "Mouse" then return player:GetMouse().Hit.Position could cause an errow to be thrown if youre not verifying that player:GetMouse().Hit isnt nil before returning Hit.Position

1 Like

Oh, I forgot that I named it “Mouse” and not “MousePosition” :sob::sob: Genuinely slipped my mind…

I also moved the OnClientInvoke callback thing right under the repeat loop just to make sure the mouse loads. The mouse DOES load, It’s just that It’s position doesn’t get sent to the server correctly for some reason.

Here’s the client script again:

local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

local events = {}

for _, z in pairs(script.Parent:GetDescendants()) do
	if z:IsA("RemoteFunction") or z:IsA("RemoteEvent") then
		events[z.Name] = z
	end
end

local uis = game:GetService("UserInputService")

local mouse

repeat
	task.wait()
	mouse = player:GetMouse()
until mouse ~= nil

events["Data"].OnClientInvoke = function(args) -- Used to be at the very top of the script, now It's under the repeat loop
	if args["Request"] == "PlayerStuff" then
		return {
			Player = player,
			Character = char
		}

	elseif args["Request"] == "Mouse" then
		return mouse.Hit.Position
	end
end

uis.InputBegan:Connect(function(i, g)
	if g then return end
	
	if i.UserInputType == Enum.UserInputType.MouseButton1 then
		events["Input"]:FireServer({
			Input = "Click",
			args = {
				MousePosition = mouse.Hit.Position
			}
		})
	end
end)

Here’s the line of code that (still) throws out the error for some reason:

ball.Velocity = (events["Data"]:InvokeClient({Request = "Mouse"}) - limbs["HumanoidRootPart"].Position).Unit * 500
1 Like

Can anybody help me with this? (Sorry for bumping)

1 Like

This is just a recommendation, use bytenet for client-server communication

1 Like

I’ll give this a try. Do you think It’ll work for my use case? (Getting the player’s mouse position whenever they click?)

1 Like

Yes, bytenet can send vector3 values and cframe