Help with building system

Hi, there! I’m trying to make a building system in Roblox, but when I fire a remote event to the server the so called “mouse position” is not labeled as “Vector3”, but as “any”. Here’s my LocalScript:

local player = game.Players.LocalPlayer
local buildingMode = false
local cursorIndicator = nil

local function toggleBuildingMode()
	buildingMode = not buildingMode
	if buildingMode then
		print("Building mode ON")
		script.Parent.TextLabel.Text = "ON"
		game.ReplicatedStorage.EnableBuild:FireServer(player)
	else
		print("Building mode OFF")
		script.Parent.TextLabel.Text = "OFF"
		game.ReplicatedStorage.DisableBuild:FireServer(player)
	end
end

script.Parent.Activated:Connect(toggleBuildingMode)

game:GetService("UserInputService").InputBegan:Connect(function(input, isProcessed)
	if not isProcessed and input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.R then
			toggleBuildingMode()
		end
	end
end)

-- Add this part to handle placing objects while in building mode
local function placeObject()
	if buildingMode then
		local inputService = game:GetService("UserInputService")
		local position = nil

		if inputService.TouchEnabled then
			local touch = inputService.TouchStarted:wait()
			if touch then
				position = touch.Position
			end
		else
			position = inputService:GetMouseLocation()
		end

		if position then
			-- Call the server to place the object
			game.ReplicatedStorage.BuildEvent:FireServer(player, position)
			print("Success")
		end
	end
end

game:GetService("UserInputService").InputBegan:Connect(function(input, isProcessed)
	if not isProcessed and (input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch) then
		placeObject()
	end
end) 

You wouldn’t need to see the ServerScript, as I’ve made no progress on that one other than the connecting to the function.

Any help appreciated! Also please point out if the position of the mouse is wrong or it seems off compared to normal building systems. Thanks!

2 Likes

(As of writing this, I am not in Roblox Studio so I won’t be able to test anything.)
When you use inputService:GetMouseLocation() you grab the mouse position relative to the Screen. This isn’t in the world space.

Try using game:GetService("Players").LocalPlayer:GetMouse()
You can access the mouse.hit.position property using this approach. However, you will need to send the data from the local script to the server with the mouse data position.

1 Like

That didn’t work. Just printing out “TouchEnabled is not a valid member of PlayerMouse “Instance””

Maybe you changed your code in the wrong place. Try replacing your function with this:

local function placeObject()
	if buildingMode then
		local inputService = game:GetService("UserInputService")
		local position = nil

		if inputService.TouchEnabled then
			local touch = inputService.TouchStarted:wait()
			if touch then
				position = touch.Position
			end
		else
			position = player:GetMouse().Hit.Position -- This is the only change
		end

		if position then
			-- Call the server to place the object
			game.ReplicatedStorage.BuildEvent:FireServer(player, position)
			print("Success")
		end
	end
end

I only changed one line of code but still use this whole function in case you changed more than needed from your script.