Please help me with this code

I’m making an ability to spawn rocks. But the problem is my rocks won’t spawn
Here’s the code:

function(player)
			local chars = {}
			local char = player.Character
            spawn(function()
				local rockystomp = game.ReplicatedStorage.CommandItems.RockyStomp:Clone()
				rockystomp.Parent = char.HumanoidRootPart
				rockystomp:Play()
				local cframe = char.HumanoidRootPart.CFrame
				local direction = cframe.LookVector
				local pos = char.HumanoidRootPart.Position
				local heightbetweenlegsandrootpart = (char.HumanoidRootPart.Position-(char.HumanoidRootPart.Position+Vector3.new(0,-(char.HumanoidRootPart.Size.Y/2+char["Left Leg"].Size.Y),0))).Magnitude
				wait(.1)
				for i=1,25 do
					local rock = Instance.new("Part",game.Workspace)
					rock.Name = "Rock"
					rock.BrickColor = BrickColor.new("Grey")
					rock.Material = Enum.Material.Pebble
					rock.Size = Vector3.new(12,14,4)
					rock.Anchored = true
					rock.CanCollide = false
					local canharm = true
					local e = -(heightbetweenlegsandrootpart+((pos+Vector3.new(0,-heightbetweenlegsandrootpart,0))-Vector3.new(pos.X,game.Workspace.Baseplate.Position.Y+(game.Workspace.Baseplate.Size.Y/2),pos.Z)).Magnitude)
					rock.CFrame = cframe * CFrame.new(0,e,-3-(3.2*i)) 
					if game.Workspace:Raycast(rock.Position,(-rock.CFrame.UpVector).Unit*10) then
						rock.CFrame = rock.CFrame * CFrame.new(0,-10,0) * CFrame.Angles(math.rad(math.random(-20,20)),math.rad(math.random(-20,20)),math.rad(math.random(-20,20)))
						game:GetService("TweenService"):Create(rock,TweenInfo.new(.15),{Position = rock.Position+Vector3.new(0,10,0)}):Play()
						rock.Touched:Connect(function(hit)
							if canharm then
							if hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
								if hit.Parent.Humanoid.Health ~= 0 then
									local hitchar = hit.Parent
									local hitplr = game.Players:GetPlayerFromCharacter(hitchar)
									if (hitplr and hitplr.IsOnMatch.Value) or (not hitplr) then
										if hitchar~= char then
										if chars[hitchar] == nil then
											chars[hitchar] = true
											hitchar.Humanoid.Health-=10
										end
										end
									end
								end
							end
							end
						end)
						spawn(function()
							wait(.5)
							canharm = false
							wait(5)
							game:GetService("Debris"):AddItem(rock,2.5)
							game:GetService("TweenService"):Create(rock,TweenInfo.new(2),{Position = rock.Position+Vector3.new(0,-15,0)}):Play()
						end)
					else
						rock:Destroy()
					end
					wait(.02)
				end
			end)
		end,

Please help me, and thank you!

1 Like

I would add prints before the if statements to print what your asking in the if. If the first if isn’t what you think it is the rock would instantly be destroyed.

2 Likes

I checked, the only problem is the statement that checks raycasting. It checks if the rock is on the grounds or not. If not, the rock’ll be destroyed. But the rocks keep being destroyed even though they are on the ground.

2 Likes

sorry for the delay! can you print(e) i think its under the ground so a raycast would always be nil.

1 Like

I raised it by +6 and it worked for me.

So if you get rid of the - its works well.

local e = (heightbetweenlegsandrootpart+((pos+Vector3.new(0,-heightbetweenlegsandrootpart,0))-Vector3.new(pos.X,game.Workspace.Baseplate.Position.Y+(game.Workspace.Baseplate.Size.Y/2),pos.Z)).Magnitude)

1 Like

I added the minus to fix the the case if the player using it somehow enlarges themselves by using another ability

also I checked raycasting before making it underground and positive e means the distance between the humanoidrootpart position and the bottom of the legs plus the distance between the bottom of the legs and the ground

If the e needs the - then you just need to raise the ray origin (I did by +6). If the ray starts underground it can’t hit the surface.

so you mean when a part is in another part, the ray cannot detect the another part?