Hello , i’m trying to make a part spawn right on the mouse cursor with a custom offset .
i don’t know how to make it but i made something similar to what i want .
do you have any idea how to make it more precise ? Thx
local Player = game.Players.LocalPlayer
local PMouse = Player:GetMouse()
task.wait(5)
local Head = Player.Character:WaitForChild("Head")
while task.wait(0.05) do
local MouseHit = PMouse.Hit.Position
local MoveToPos = MouseHit - Head.Position
local XPos
local YPos
local ZPos
if string.sub(tostring(MoveToPos.Unit.X), 1, 1) == "-" then
XPos = string.sub(tostring(MoveToPos.Unit.X), 1, 4)
else
XPos = string.sub(tostring(MoveToPos.Unit.X), 1, 3)
end
if string.sub(tostring(MoveToPos.Unit.Y), 1, 1) == "-" then
YPos = string.sub(tostring(MoveToPos.Unit.Y), 1, 8)
else
YPos = string.sub(tostring(MoveToPos.Unit.Y), 1, 7)
end
if string.sub(tostring(MoveToPos.Unit.Z), 1, 1) == "-" then
ZPos = string.sub(tostring(MoveToPos.Unit.Z), 1, 4)
else
ZPos = string.sub(tostring(MoveToPos.Unit.Z), 1, 3)
end
local tempPart = Instance.new("Part")
tempPart.Position = Vector3.new(Head.Position.X + ( XPos * 10 ),Head.Position.Y + ( YPos * 10 ),Head.Position.Z + ( ZPos * 10 ))
tempPart.Size = Vector3.new(0.5,0.5,0.5)
tempPart.Material = Enum.Material.SmoothPlastic
tempPart.Anchored = true
tempPart.CanCollide = false
tempPart.Parent = workspace
game:GetService("Debris"):AddItem(tempPart,0.05)
-- warn(XPos--[[,YPos,ZPos]])
end
After Editing the script it became more stable ( the part didn’t glitch up and down Like in the vid)but still not precise enough.
What exactly are you trying to do, should the part be on the location where the mouse is hitting or do you want to do it with an maximum range? I’d also recommend using the run-service instead of a while loop so you can add more stuff without the loop blocking it from happening. When getting parts of the players character you don’t need to wait you can call “.CharacterAdded:Wait()” on the player because the function returns the character as soon as it has loaded.
If you were to do it with an maximum set range it would look something like this:
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local character = player.CharacterAdded:Wait()
local head = character:WaitForChild("Head")
local runService = game:GetService("RunService")
local maxRange = 10 -- Range is given in studs
runService.Heartbeat:Connect(function()
local mouseCFrame = mouse.Hit
local mousePosition = mouseCFrame.Position
local distance = (mousePosition - head.Position).Magnitude
local tempPart = Instance.new("Part", game.Workspace)
tempPart.Size = Vector3.new(0.5,0.5,0.5)
tempPart.Material = Enum.Material.SmoothPlastic
tempPart.Anchored = true
tempPart.CanCollide = false
if distance > maxRange then
tempPart.Position = head.Position + mouseCFrame.LookVector * maxRange
else
temPart.Position = mousePosition
end
end)
If you don’t want a maximum range you can just set it to “math.huge” or a big number.
If you just want the part to spawn on the cursor with a basic offset, try using something like this instead:
local Player = game.Players.LocalPlayer
local PMouse = Player:GetMouse()
local Head = Player.Character:WaitForChild("Head")
local tempPart = Instance.new("Part")
tempPart.Size = Vector3.new(0.5, 0.5, 0.5)
tempPart.Material = Enum.Material.SmoothPlastic
tempPart.Anchored = true
tempPart.CanCollide = false
tempPart.Parent = workspace
while true do
local MouseHit = PMouse.Hit.Position
local Offset = Vector3.new(0, 10, 0) -- Adjust the offset as per your requirements
local SpawnPosition = MouseHit + Offset
tempPart.Position = SpawnPosition
wait(0.05)
end
You can adjust the offset (X/Y/Z) inside the while loop as needed.