changing it back did fix it, I still need to find a way to place the box down at the end of the raycast.
Thanks for the help though!
12th line
you tried doing math with an instance (the head)
do player.Character.Head.Position
maybe itâll work better than .CFrame
, since cframe + vector3 works a little bit differently than vector3 and vector3
yes, edit the original codeblock you sent
anyways
some general tips
use .Position instead of .p they work the same, but .p is deprecated
also .Position just makes more sense lol
also replace .unit with .Unit for the same reason lol
Players is a service, so you can do game:GetService("Players")
. itâs better to do that for reasons that you can find in devforum
anyways if you want to use workspace:Raycast()
, hereâs a codeblock lol
local beamradius = 1
local Holding = false
local Held = nil
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
------------------------------------
local function MakeRayVisible(ray)
local midpoint = player.Character.Head + mouse.Hit.Position/2
local raypart = Instance.new("Part")
raypart.Parent = workspace
raypart.Anchored = true
raypart.CFrame = CFrame.new(midpoint, player.Character.Head)
raypart.Size = Vector3.new(beamradius, beamradius, mouse.Hit.Position.Magnitude)
raypart.Material = Enum.Material.Neon
raypart.BrickColor = BrickColor.new("Light red")
return raypart
end
------------------------------------
local function performRaycast()
if not player.Character or not player.Character:FindFirstChild("Head") then return end -- make sure character actually has a head
-------------------------------------
if Holding==true then
MakeRayVisible()
Held.Position = mouse.Hit.Position - player.Character.Head.Position
Held.PartToPlayerWeld:Destroy()
Holding = false
Held = nil
--------------------------------------
else
local rayp = RaycastParams.new()
rayp.FilterDescendantInstances = {player.Character}
local origin, direction = player.Character.Head.Position, (mouse.Hit.Position - player.Character.Head.Position).Unit * 20
local ray = workspace:Raycast(origin, direction)
if ray and ray.Instance then
local Part = ray.Instance
MakeRayVisible()
Held = Part
print("Hit:".. Part.Name)
local weld = Instance.new("Weld")
weld.Name = "PartToPlayerWeld"
weld.Part0 = player.Character.Head
weld.Part1 = Part
weld.Parent = Part
Holding = true
end
end
end
mouse.Button1Up:Connect(performRaycast)
Thank you so much for the code block! It fixed a lot of issues (and showed me how many times I forgot to add .Position) Fortunately the part that goes around the raycast works! unfortunately, it doesnât do the right thingâŚ
Again, thanks for the help! Itâs helped get the dropping to work, the raycast unfortunately doesnât work as well for some reason.
I will post an update of the script once Iâm done modifying it
After moving some stuff around, working on the code, (and probably unintentionally breaking something) I have a problem⌠despite there being parameters to stop this, the raycasts get stopped on my characterâs hats before being able to detect anything.
The dropping seems to work fine though after making it hold a box before loading into a playtest
And hereâs the updated code
local Holding = false
local Held = nil
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
------------------------------------
local function performRaycast()
local rayp = RaycastParams.new()
rayp.FilterType = Enum.RaycastFilterType.Include
rayp.FilterDescendantsInstances = {workspace.Boxes}
local origin = player.Character.Head.Position
local direction = mouse.Hit.Position
if not player.Character or not player.Character:FindFirstChild("Head") then return end
-------------------------------------
if Holding == true then
Held.Position.Y = mouse.Hit.Position.Y
Held.Position = Held.Position + 1
Held.PartToPlayerWeld:Destroy()
Holding = false
Held = nil
--------------------------------------
else
local ray = workspace:Raycast(origin, direction)
if ray and ray.Instance then
local Part = ray.Instance
print(Part.Name)
if Part.Parent == workspace.Boxes then
Held = Part
print("Hit:".. Part.Name)
local weld = Instance.new("Weld")
weld.Name = "PartToPlayerWeld"
weld.Part0 = player.Character.Head
weld.Part1 = Part
weld.Parent = Part
print("Welded")
Holding = true
end
end
end
end
mouse.Button1Up:Connect(performRaycast)
I started rewriting the raycasting in a separate script and have ran into a few bugs, the raycast doesnât seem to go in the right direction, and it cannot find the characterâs head despite it have been working in other scripts
local players = game:GetService("Players")
local player = players.LocalPlayer
local mouse = player:GetMouse()
------------------------------------------------
local rayOrigin = player.Character.Head.Position
local rayDirection = mouse.Hit.Position
local rayp = RaycastParams.new()
rayp.FilterDescendantsInstances = {workspace.Boxes}
rayp.FilterType = Enum.RaycastFilterType.Include
local function performRaycast(rayOrigin, rayDirection)
local rayResult = workspace:Raycast(rayOrigin, rayDirection)
if rayResult then
print("Instance:", rayResult.Instance)
print("Position:", rayResult.Position)
print("Distance:", rayResult.Distance)
print("Material:", rayResult.Material)
print("Normal:", rayResult.Normal)
else
warn("No raycast result!")
end
end
mouse.Button1Up:Connect(performRaycast)