Passing position through a RemoteEvent, output returns an error saying that "Vector3 expected, got Instance"?

Okay, so i’m trying to create a shovel that can dig out terrain. It has a adjustable size value in the script, and bla bla bla, but for some reason, when i pass over the Vector3 to the server script, it says it’s an instance???

LocalScript

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local tool = script.Parent

local replicatedStorage = game:GetService("ReplicatedStorage")
local remote = replicatedStorage:WaitForChild("ShovelEvent")

local size = 3

local function onMouseClick()
	local mouseHit = mouse.Hit
	if mouseHit then
		local position = mouseHit.Position
		print("pos:", position)

		remote:FireServer(position, size)
	end
end

tool.Activated:Connect(onMouseClick)

ServerScript

local replicatedStorage = game:GetService("ReplicatedStorage")
local terrain = workspace.Terrain

local digTerrainEvent = replicatedStorage:WaitForChild("ShovelEvent")

local function digTerrain(position, size)
	local cframe = CFrame.new(position)
	terrain:FillBall(cframe.Position, size, Enum.Material.Air)
end

digTerrainEvent.OnServerEvent:Connect(digTerrain)

It prints out the position just fine, but that SPECIFIC part just doesn’t work. How do i fix this?

1 Like

When you fire a remote event from the client, the first parameter is always the player. So position is actually the player who fired it, and size is your position.

To fix:

local function digTerrain(plr, position, size)
end

Tried that, error still persisted…
ServerScriptService.Server:7: invalid argument #1 to ‘new’ (Vector3 expected, got Instance)

well after adding the plr part can I see your code?

Okay, here:

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local tool = script.Parent

local replicatedStorage = game:GetService("ReplicatedStorage")
local remote = replicatedStorage:WaitForChild("ShovelEvent")

local size = 3

local function onMouseClick()
	local mouseHit = mouse.Hit
	if mouseHit then
		local position = mouseHit.Position
		print("pos:", position)
		local vec3Pos = Vector3.new(position)

		remote:FireServer(player, vec3Pos, size)
	end
end

tool.Activated:Connect(onMouseClick)

And the server script:

local replicatedStorage = game:GetService("ReplicatedStorage")
local terrain = workspace.Terrain

local digTerrainEvent = replicatedStorage:WaitForChild("ShovelEvent")

local function digTerrain(player, position, size)
	local cframe = CFrame.new(position)
	terrain:FillBall(cframe.Position, size, Enum.Material.Air)
end

digTerrainEvent.OnServerEvent:Connect(digTerrain)

I’ll remove the vec3Pos part since it isn’t really necessary. Just forget it exists.

This makes no sense. Vector3.new wants 3 numbers, not a Vector3.

should size be a vector3? Or is that just for terrain:FillBox(Or whatever its called)

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local tool = script.Parent

local replicatedStorage = game:GetService("ReplicatedStorage")
local remote = replicatedStorage:WaitForChild("ShovelEvent")

local size = 3

local function onMouseClick()
	local mouseHit = mouse.Hit
	if mouseHit then
		local position = mouseHit.Position
		print("pos:", position)

		remote:FireServer(player, position, size)
	end
end

tool.Activated:Connect(onMouseClick)

No, this is wrong. You don’t pass player, you just get player on the server end.

  1. fire a remote from the client, with parameters
  2. receive the event on the server, with the player, and the parameters
1 Like

Alright, it works now, thanks for the help.

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