Hi, I have a problem with my script, it behaves strangely. I need that when the player takes the tool, the “shadow” of the block of his choice is displayed, and convert to the cursor position using Rays for a more correct position. But something is clearly going wrong, and the position of a “shadow” is multiplied by two. That is, let’s say that I have moved my cursor at position x10 y15 z20, and “shadow” is moved at x20 y30 z40.
Here’s a GIF:
Here’s a script:
local Player = game.Players.LocalPlayer
local Char = Player.Character
local Mouse = Player:GetMouse()
equipped = false
function FindPos(ignoreList)
if Mouse.Target ~= nil and Shadow ~= nil then
local unitRay = workspace.CurrentCamera:ScreenPointToRay(Mouse.x, Mouse.y)
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {Char, Shadow.PrimaryPart}
local ray = workspace:Raycast(unitRay.Origin, unitRay.Direction * 250, raycastParams)
if ray then
return ray.Position, ray.Instance, ray.Normal
end
end
end
script.Parent.Parent.Equipped:Connect(function()
equipped = true
Shadow = game.ReplicatedStorage.Blocks.Wood:Clone()
Shadow.Parent = workspace
Shadow.Root.BrickColor = BrickColor.Blue()
Shadow.Root.Transparency = 0.5
Shadow.Root.CanCollide = false
Shadow.Root.CastShadow = false
Shadow.Root.Anchored = true
Shadow.PrimaryPart = Shadow.Root
Shadow.Name = "Shadow"
local x = math.floor(Mouse.Hit.X + .5)
local y = math.floor(Mouse.Hit.Y + .5)
local z = math.floor(Mouse.Hit.Z + .5)
local Normal = FindPos()
local NewPos = Vector3.new(x, y, z)
local FinPos = NewPos + Normal
Shadow.Root.CFrame = CFrame.new(FinPos)
Mouse.TargetFilter = Shadow.Root
while true do
wait()
if equipped == true and Shadow.Root then
local x = math.floor(Mouse.Hit.X + .5)
local y = math.floor(Mouse.Hit.Y + .5)
local z = math.floor(Mouse.Hit.Z + .5)
local Normal = FindPos()
local NewPos = Vector3.new(x, y, z)
if NewPos and Normal then
local FinPos = NewPos + Normal
Shadow.Root.CFrame = CFrame.new(FinPos)
--game:GetService("TweenService"):Create(Shadow.Root, TweenInfo.new(0.1), {CFrame = FinPos}):Play()
end
else
Shadow:Destroy()
end
end
end)
script.Parent.Parent.Unequipped:Connect(function()
equipped = false
end)
Thanks for any help!