Having issue with an spawnlocation spawner tool

I’am trying to make an spawnlocation spawner tool for an friend, i have came up with this:

local plr = game.Players.LocalPlayer
local m = plr:GetMouse()
local CF = m.Hit

script.Parent.Equipped:Connect(function()
	script.Parent.Activated:Connect(function()
		local SP = Instance.new("SpawnLocation")
		SP.Parent = workspace
		SP.CFrame = CFrame.new(CF)
	end)
end)

--LocalScript

The issue i’am having is on the SP.CFrame = CFrame.new(CF), having this error: 15:26:22.482 - Players.PhoenixRessusection.Backpack.CreateSpawnTool.Script:4: bad argument #1 (Vector3 expected, got Object)

I tried doing some things, but i’am not being able to fix this at all.

Also, this item is supposed to be an gamepass item, and it should only work once, so don’t say to me that i should use remote events.(+ Other thing.)

m.Hit is itself a CFrame, so you don’t need the CFrame.new constructor:

SP.CFrame = CF

I just used it cause both ways i was getting the error.

Edit: Actually, now it stopped the error, but it creates the brick in the 0,0,0 position.

You want to also define CF inside the event listener, otherwise you’re just initializing it to one value and never changing it.

local connection

script.Parent.Equipped:Connect(function(mouse)
	connection = script.Parent.Activated:Connect(function()
		if (mouse.Target ~= nil) then -- remove this check if you want to spawn bricks in the sky
			local SP = Instance.new("SpawnLocation")
			SP.CFrame = CFrame.new(mouse.Hit.Position)
			SP.Parent = workspace
		end
	end)
end)

script.Parent.Unequipped:Connect(function()
	if (connection) then
		connection:Disconnect()
		connection = nil
	end
end)
1 Like

Oh, i didn’t actually knew that Tool.Equipped had the mouse as the/an parameter, thank you for that, and i also didn’t knew that you could use an Vector3 value in an CFrame, welp, ok.

A CFrame has a Position property that just gets its location without any rotation, since mouse.Hit will be angled in the direction your camera is pointing.