please only give answers you have tried in the past, and no CHATGPT responses - please.
So, we want to drop a landmine to the ground. We are using a raycast to “find” the ground so we can set the landmine’s CFrame. However, the Raycast isn’t finding anything when it shoots through the baseplate. The baseplate is not blacklist/excluded. Here’s our code if it helps:
local tool = script.Parent
local replicatedS = game:GetService("ReplicatedStorage")
local function onActivated()
local deployedMineClone = replicatedS:WaitForChild("Landmine"):Clone()
local rayOrigin = tool.Handle.Position--Vector3.new(hrp.CFrame)
--local rayOrigin = Vector3.new(hrp.CFrame)
local rayDirection = Vector3.new(0, -100, 0)
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {script.Parent.Parent}
raycastParams.IgnoreWater = true
--local raycastResult = workspace:Raycast(rayOrigin, (rayDirection).Unit * 10, raycastParams)
local raycastResult = workspace:Raycast(rayOrigin,rayDirection, raycastParams)
--this function takes the ray as a parameter. Below, it gets the midpoint.
local MidPoint = tool.Handle.Position + (tool.Handle.Position + Vector3.new(0, -100, 0))/2
--below it creates the part and returns it. I'm sure you're familiar with properties
local Part = Instance.new("Part")
Part.Parent = workspace
Part.Anchored = true
--Part.CFrame = CFrame.lookAt(MidPoint, tool.Handle.Position)
Part.CFrame = CFrame.lookAt(MidPoint, tool.Handle.Position)
Part.Size = Vector3.new(1, 1, 100)
if raycastResult then
print(raycastResult.Instance.Position)
--
deployedMineClone:SetPrimaryPartCFrame(raycastResult.Instance) --you can also edit the orientation though the second parameter of the CFrame
deployedMineClone.Parent = game.Workspace
deployedMineClone.Top.CanTouch = false
deployedMineClone.Mid.CanTouch = false
deployedMineClone.Bottom.CanTouch = false
wait(2)
deployedMineClone.Top.CanTouch = true
deployedMineClone.Mid.CanTouch = true
deployedMineClone.Bottom.CanTouch = true
deployedMineClone.Top.Anchored = true
deployedMineClone.Mid.Anchored = true
deployedMineClone.Bottom.Anchored = true
--tool:Destroy()
end
end
tool.Activated:Connect(onActivated)
edit:
this code is in the folder tree like this:
workspace/
landmine tool (a tool object)/
DeployMineServerScript
also, we’ve had lots of success with raycast in the past - we have bots that will shoot at your character and stuff that we worked on and adjusted. I just can’t get it to shoot straight down and see the baseplate.
You use print, so where and what is the script printing? This code is very unorganized and I can’t tell what is going on. function RayToPart is also unused.
thank you VERY MUCH for trying to help. let me give this a shot !
IT WORKS. Thank you very much - we were working on this for an entire week or so I think.
I am going back to study what you did now so I can use this better. We’ve been coding a while, and really appreciate when someone is kind enough to help us with the few parts we do not understand ~! So thank you so much !
I’m going to compare and find out. and see how you optimized it. we are still working on elegance. the code above was especially messy because it wasn’t in its final form. a lot of time our code goes that way. working on it ! You rule !
I’ll have to learn what that is Sometimes we have trouble keeping up with options because we code/build so much we don’t have time to see the new stuff…even when its not that new (been at this for about 4 or 5 years now)
OK, some differences I see in your code as we study it, but I’m not sure why it works like this, instead of as we did it. It seems you kind of do the same thing…but ours fails. Some differences we note:
for the origin, you use the character primarypart.position in stead of humanoidRootPart.position or the tools handle.position
you reference the character in a way I don’t totally undersatnd, as
local char: Model = script.Parent.Parent
your direction is vector3.new(0,-250,0) whereas ours used -100
you use the cframe of the result from the raycast, instead of just its instance - noted - even though this wasn’t causing our fail since it was inside the if statement… but thanks, that was an error for sure.
any reason you do item #2 this way? – char: Model = script.parent.Parent… I’m not used to the : Model = ?
Hey strange thing - It’s working better, but for some reason it’s putting the cloned landmine really close to the origin. No matter where we activate the tool in 3d space.
stranger still - the cloned Mine doesn’t have original position at origin, so that’s not the reason… we are still working on it, but really feel you have us close to an answer.
Ok just figured it out. We are using
result.cframe - it hits the baseplate. and the baseplates position is at 0,0,0 so it puts our landmine at the center of the baseplate, not where we touched it. Got it.
It’s the same way you referenced it, I just put it in a variable!
The : Model is simplying just adding a type to the variable. This will give you autocomplete so it’s easier to code. Without that, it give you autocomplete for the baseclass Instance.
We had just changed it to:
deployment:SetPrimaryPartCFrame(CFrame.new(result.Postion))
before reading your response. we will try what you use now! (our idea didn’t work, so lets try yours.)
…
Just tried it, and it works ! I’ll have to learn about this now (pivotTo) too. Kind thanks for the link to Typechecking. If more folks on this forum were as helpful as you, it wouldn’t have taken us 4 years to learn what we’ve learned.
PivotTo does the same thing as SetPrimaryPartCFrame, except the Pivot can be changed. The default pivot is the primary part cframe or the cframe of the model bounding box.