Problem with raycasting script

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to fix a little issue on my raycasting code that im using to make a mine/claymore thing in my game, the basic raycast works, if the raycast detects something in the radius , its gonna blow up, but the damaging part dont works, keep in mind i just started to learn raycasting.

  2. What is the issue? Include screenshots / videos if possible!
    The raycast cant get the parent of what it hits, making damaging a player with it not possible
    parent

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Tried changing a few parts in the script but nothing worked, and devforum is always a helping hand.
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

   while wait(0.1) do 
	local Hit, Position = game.Workspace:Raycast(Part.Position,Vector3.new(0,20,0))

	if Hit then
		wait(0.2)
		print(Hit)
		local Hit2, Position = game.Workspace:Raycast(Part.Position,Vector3.new(40,40,40))
			beep:Play()
			weak.Enabled = true
			strong.Enabled = true
			effect.Enabled = true
			lig.Enabled = true
			ligc.Enabled = true
			ligc2.Enabled = true
			light.Enabled = true
			script.Parent.Dist1:Play()
			wait(0.1)
			weak.Enabled = false
			strong.Enabled = false
			effect.Enabled = false
			lig.Enabled = false
			ligc.Enabled = false
			ligc2.Enabled = false
		    light.Enabled = false
		if Hit2 ~= nil then
			wait(0.2)
			if Hit2.Parent:FindFirstChild("Humanoid") then
				Hit2.Parent.Humanoid:TakeDamage(180)
			    end
		    end
			wait(0.5)
			script.Parent.Transparency = 1
			script.Parent.CanCollide = false
		    wait(0.3)
		    script.Parent.Explode.Value = true
			script.Parent:Destroy()
	end
end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

In order to get the parent of the Instance that a raycast hits, you need to get the Instance of the RaycastResult and then get the parent of that Instance. Here is an example, using your script above.

local Result = game.Workspace:Raycast(Part.Position, Vector3.new(0,20,0)) --The RaycastResult
local Hit = Result.Instance --The Instance that the Raycast hits
local Parent = Hit.Parent --The Parent of the Instance

Tried that out, now i get a new error in the console
new
I put the new part of the code like this =
ererer

You need to check if there was a return from workspace:Raycast before attempting to index it.

local hit = workspace:Raycast(part.Position, ...)
if hit then
    ...

That fixed the problem, but just a question, is there any way to make the raycast cover more of a " circular " area than a line, as right now you gotta be standing right infront of it or at the center of the top of the part to actually get damaged.

Nope, unfortunately there aren’t any ways to ray cast in any shape other than a line unfortunately. There could be a community module that allows this but I’m not too sure, I know it isn’t natively supported though.

Alright then, guess imma stick with the lines, or do some weird thing to make it cover more space, devforum always helps me out when i have a error in my incredibly starter scripts!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.