Problem with rocks appearing when the enemy lands

Local Script:

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local player = game.Players.LocalPlayer

local uis = game:GetService("UserInputService")
local ts = game:GetService("TweenService")

local remote = game:GetService("ReplicatedStorage").Combat
local remoteRock = game:GetService("ReplicatedStorage"):WaitForChild("Rock")

local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Whitelist
params.FilterDescendantsInstances = {workspace.Map}

local lastTimeM1 = 0
local lastM1End = 0
local combo = 1

local canAir = true

local punchAnims = {
	'rbxassetid://13111415464', -- 1
	'rbxassetid://13111417311', -- 2
	'rbxassetid://13111419879', -- 3
	'rbxassetid://13111422893', -- 4
	'rbxassetid://13111431501', -- 5
}


local airAnims = {
	'rbxassetid://13103638685', -- KICK UP
	'rbxassetid://13103652945', -- KICK DOWN
}

local function hb(size, Cframe, ignore, char)
	local hb = Instance.new("Part", workspace.Fx)
	hb.Anchored = true
	hb.CanCollide = false
	hb.Transparency = 0.6
	hb.Name = "hb"
	hb.Material = Enum.Material.ForceField
	hb.CanQuery = false
	hb.Size = size
	hb.CFrame = Cframe
	
	local con 
	con = hb.Touched:Connect(function()
		con:Disconnect()
	end)
	
	local lasttarg
	
	for i, v in pairs(hb:GetTouchingParts()) do
		if v.Parent:FindFirstChild("Humanoid") and table.find(ignore, v.Parent) == nil then
			if lasttarg then
				if (lasttarg.Position - char.PrimaryPart.Position).Magnitude > (v.Position - char.PrimaryPart.Position).Magnitude then
					lasttarg = v.Parent.PrimaryPart
				end
			else
				lasttarg = v.Parent.PrimaryPart
			end
		end
	end
	
	hb:Destroy()
	if lasttarg then
		return lasttarg.Parent
	else
		return nil
	end
end

local function rock(data)
	local currentTime = tick()
	local rockRaycastResult
	repeat
		rockRaycastResult = workspace:Raycast(data.Target.PrimaryPart.Position, data.Direction, params)
		--print(rockRaycastResult)
		wait()
	until tick() - currentTime > 5 or rockRaycastResult ~= nil
	
	if rockRaycastResult then
		for i = 0, 14 do
			local part = Instance.new("Part", workspace.Fx)
			part.Size = Vector3.new(4, math.random(10, 20) / 10, math.random(10, 20) / 10)
			part.Anchored = true
			part.CFrame = CFrame.new(rockRaycastResult.Position, rockRaycastResult.Position + rockRaycastResult.Normal)
			part.CFrame = part.CFrame * CFrame.Angles(math.rad(90), math.rad(i * 360/14), 0) * CFrame.new(0, 0, -4 * 2) * CFrame.Angles(math.rad(35), 0, 0)
			part.CanQuery = false
			part.CanCollide = false
			part.CanTouch = false
						
			local result = workspace:Raycast(part.Position + rockRaycastResult.Normal * 4, rockRaycastResult.Normal * -5, params) -- BUG
			print(result)
			if result then
				part.Position = result.Position
				part.Material = result.Material
				part.Color = result.Instance.Color
				print("1")
			else
				part:Destroy()
				print("2")
			end
			
			part.Position = part.Position + rockRaycastResult.Normal * -4
			ts:Create(part, TweenInfo.new(.2, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out, 0, false, 0), {Position = part.Position + rockRaycastResult.Normal * 4}):Play()
			
			spawn(function()
				game.Debris:AddItem(part, 4)
				wait(3)
				ts:Create(part, TweenInfo.new(.5, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out, 0, false, 0), {Position = part.Position + rockRaycastResult.Normal * -4}):Play()
			end)
			
			if i % 3 < 2 and result then
				local rubble = part:Clone()
				rubble.Size = Vector3.new(math.random(10, 20) / 20, math.random(10, 20) / 20, math.random(10, 20) / 20)
				rubble.Position = result.Position + rockRaycastResult.Normal * 4
				rubble.Material = result.Material
				rubble.Color = result.Instance.Color
				
				rubble.Parent = workspace.Fx
				rubble.Anchored = false
				rubble.CanCollide = true
				
				local bv = Instance.new("BodyVelocity", rubble)
				bv.Velocity = Vector3.new(math.random(-40, 40), 30, math.random(-40, 40))
				bv.MaxForce = Vector3.new(99999, 99999, 99999)
				bv.Name = "Velocity"
				
				game.Debris:AddItem(bv, .1)
				game.Debris:AddItem(rubble, 4)
				
				spawn(function()
					wait(2)
					
					ts:Create(rubble, TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out, 0, false, 0), {Transparency = 1}):Play()
				end)
				
				rubble.Transparency = 0
			end
		end
	end
end

uis.InputBegan:Connect(function(input, gpe)
	if gpe then return end
	
	if input.UserInputType == Enum.UserInputType.MouseButton1 and tick() - lastTimeM1 > .3 and tick() - lastM1End > .7 then
		if tick() - lastTimeM1 > .7 then
			combo = 1
		end
		
		lastTimeM1 = tick()
		
		local animation = Instance.new("Animation", workspace.Fx)
		local air = nil
		
		if uis:IsKeyDown("Space") and combo == 3 and canAir then
			canAir = false
			animation.AnimationId = airAnims[1]
			air = "Up"
		elseif not uis:IsKeyDown("Space") and combo == 4 or 5 and not canAir then
			animation.AnimationId = airAnims[2]
			air = "Down"
		else
			animation.AnimationId = punchAnims[combo]
		end
		
		local load = humanoid:LoadAnimation(animation)
		load:Play()
		
		animation:Destroy()
		
		local hitTarg = hb(Vector3.new(4, 6, 4), character.PrimaryPart.CFrame * CFrame.new(0, 0, -3), {character} ,character)
		
		if hitTarg then
			local data = {
				["Target"] = hitTarg,
				["Character"] = character,
				["Combo"] = combo,
				["Air"] = air,
				["Action"] = "ml",
			}
			
			remote:FireServer(data)
		end
		
		if combo == #punchAnims then
			combo = 1
			lastM1End = tick()
		else
			combo += 1
		end
		
		humanoid.WalkSpeed = 0
		wait(.4)
		humanoid.WalkSpeed = 13
	end
end)

remote.OnClientEvent:Connect(function(data)
	if data.Action == "Rock" then 
		rock(data)
	end
end)

humanoid.StateChanged:Connect(function(old, new)
	if new == Enum.HumanoidStateType.Landed then
		canAir = true
	end
end)

uis.JumpRequest:Connect(function()
	if tick() - lastTimeM1 < 1 then
		humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
	else
		humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, true)
	end
end)

Script:

local remote = game:GetService("ReplicatedStorage"):WaitForChild("Combat")
local remoteRock = game:GetService("ReplicatedStorage"):WaitForChild("Rock")

remote.OnServerEvent:Connect(function(client, data)
	if data.Action == "ml" then
		data.Target.Humanoid:TakeDamage(5)
		if data.Air == "Up" then
			local bp = Instance.new("BodyPosition")
			bp.Position = data.Character.PrimaryPart.Position + Vector3.new(0, 30, 0)
			bp.P = 1200
			bp.MaxForce = Vector3.new(99999, 99999, 99999)
			bp.D = 200
			bp.Name = "Position"
			bp.Parent = data.Character.PrimaryPart
			game.Debris:AddItem(bp, 1)
			
			local bp = Instance.new("BodyPosition")
			bp.Position = data.Target.PrimaryPart.Position + Vector3.new(0, 30, 2)
			bp.P = 1200
			bp.MaxForce = Vector3.new(99999, 99999, 99999)
			bp.D = 200
			bp.Name = "Position"
			bp.Parent = data.Target.PrimaryPart
			game.Debris:AddItem(bp, 1)
						
		elseif data.Air == "Down" then
			for i, v in	 pairs(data.Target.PrimaryPart:GetChildren()) do
				if v:IsA("BodyMover") then
					v:Destroy()
				end
			end
						
			local bv = Instance.new("BodyVelocity", data.Target.PrimaryPart)
			bv.Velocity = (data.Character.PrimaryPart.CFrame.LookVector * 1 - Vector3.new(0, 2, 0)) * 25
			bv.MaxForce = Vector3.new(99999, 99999, 99999)
			bv.Name = "Velocity"
			game.Debris:AddItem(bv, .2)
			
			data.Action = "Rock" -- IT ISN'T WORKING
			data.Direction = (data.Character.PrimaryPart.CFrame.LookVector * 1 - Vector3.new(0, 2, 0)) * 1 -- IT ISN'T WORKING
			remote:FireAllClients(data) -- IT ISN'T WORKING
			
						
		elseif data.Combo == 5 then
			
						
			for i, v in pairs(data.Target.PrimaryPart:GetChildren()) do
				if v:IsA("BodyMover") then
					v:Destroy()
				end
			end
			
			local bv = Instance.new("BodyVelocity", data.Character.PrimaryPart)
			bv.Velocity = data.Character.PrimaryPart.CFrame.LookVector * 10
			bv.MaxForce = Vector3.new(99999, 99999, 99999)
			bv.Name = "Velocity"
			game.Debris:AddItem(bv, .2)
			
			local bv = Instance.new("BodyVelocity", data.Target.PrimaryPart)
			bv.Velocity = data.Character.PrimaryPart.CFrame.LookVector * 75
			bv.MaxForce = Vector3.new(99999, 99999, 99999)
			bv.Name = "Velocity"
			game.Debris:AddItem(bv, .2)
		else
			local bv = Instance.new("BodyVelocity", data.Character.PrimaryPart)
			bv.Velocity = data.Character.PrimaryPart.CFrame.LookVector * 10
			bv.MaxForce = Vector3.new(99999, 99999, 99999)
			bv.Name = "Velocity"
			game.Debris:AddItem(bv, .2)

			local bv = Instance.new("BodyVelocity", data.Target.PrimaryPart)
			bv.Velocity = data.Character.PrimaryPart.CFrame.LookVector * 10
			bv.MaxForce = Vector3.new(99999, 99999, 99999)
			bv.Name = "Velocity"
			game.Debris:AddItem(bv, .2)
		end
	end
end)

So what the problem is, on line 93 of the local script when I try to get the result, it says result = nil

And the line if result doesn’t work, so

local result = workspace:Raycast(part.Position + rockRaycastResult.Normal * 4, rockRaycastResult.Normal * -5, params) -- BUG
			print(result) -- IN HER I HAVE NIL
			if result then
				part.Position = result.Position
				part.Material = result.Material
				part.Color = result.Instance.Color
				print("1") -- THIS THINK ISN'T PRINT
			else
				part:Destroy()
				print("2") -- IT'S PRINT
			end

So the problem is that if result = nil, then part, is destroyed and no stones appear, how do I fill result then?

If anything, I did it all from the guide here’s a link to it there is a man everything works: Roblox Advanced Combat Tutorial Part 2 - YouTube

Are you pointing the raycast in the correct direction? If it points in a direction where nothing is hit by the ray then won’t it’s result be nil?

It seems like the raycast is aimed at the right position, at least in the guide it is the same and everything works.

although I don’t know what the issue is I can say that IsKeyDown is deprecated and you can instead use InputBegan and InputEnded. video tutorials can get outdated quickly so be sure the code isn’t outdated next time you look for a video.

I made sure the code is not obsolete, the video is 8 months old. Could it be that the problem is something else?

I believe the issue is that local params does not have anything within it’s parameters.

What should I do then, you think print doesn’t output just because it’s empty, then what should I do because I have to fill the result

fill it with a large number so it covers a large area just so that way to see if it works. if it does work then I would recommend coming up with the correct equation for it or maybe the equation is in the youtube video. if it does not work then the variable is either in the wrong place or I have no idea.

Raycasting | Roblox Creator Documentation
WorldRoot | Roblox Creator Documentation
These links to the create.roblox site should explain what the Raycast result should have in it.

As far as IsKeyDown being used in the tutorial we only know it’s deprecated. Whoever made the tutorial may still be using it though. Always consider the source of tutorials and online information because half the time online info is not 100% correct.

I’d suggest doing a test inside a large box of 6 Parts, labelled Top, Right, Front etc. and put the player inside the box. Get the name of the Part from the raycast and print it. That way if the RayCast is pointing at the Top or Left and returning something that isn’t nil at least you find out if that’s why it returns nil.

1 Like

try changing that “-5” to a bigger number, like “-50”

So I changed it to -50, and I also changed it to 40 another value. But the result is still nil.

Okay, I wrote like this… But the result is still nil:

local result = workspace:Raycast(part.Position + rockRaycastResult.Normal * 40, rockRaycastResult.Normal * -50, params) -- BUG

try putting math.rad() and putting the plain straight numbers inside of the parameters of math.rad().

@Dssall2 i would also recommend what @Scottifly is saying to do.

I do not really understand what he means, because I have the result shows where the enemy landed (as I understand it), and if I put blocks, it does not help, no? Or I do not understand something?

and can you give me an example of exactly where to put math.rad()?

sure. local result = workspace:Raycast(part.Position + rockRaycastResult.Normal * math.rad(40), rockRaycastResult.Normal * math.rad(-50), params) -- BUG

basically what @Scottifly is saying is some people still use IsKeyDown and other deprecated objects. he is saying to just do a regular raycast in a seperate script and the links he provided show what a raycast is supposed to look like.

I did as you said but the result is still nil

also remove the numbers from raycastparams as i realise what you were trying to do