How to make a system that cancels a function when behind a wall or if hes not in a certain range?

So, im making a gun system inspired by the Isle game, and now im making the damage thing part, but i need help making a system that checks if the selected enemy is behind a wall before shooting, and if the player is in a certain range, i already tried using ray cast and magnitude (for the range thing) documentaries, tutorials but i cant find out how to do it

Could anyone help me?

The Script

local tool = script.Parent
local remote = tool:WaitForChild("OnShoot")
local shoot_part = tool:WaitForChild("FirePart")
local Config = tool:WaitForChild("Config")
local Damage = Config.Damage.Value
local SFX = tool:WaitForChild("Handle")
local Particles = shoot_part:WaitForChild("Light", "Smoke")

local Shooting = false

remote.OnServerEvent:Connect(function(player, target)
	if target:IsA("BasePart") and target.Parent:FindFirstChild("ZombieNoid") then
		if not Shooting and Config.Ammo.Value > 0 then
			Config.Ammo.Value -= 1
			SFX.Shoot:Play()
			Particles.Enabled = false
			wait(2)
			--  now if the player or the enemy goes behind a wall how do i make it not shoot
		end
	end
end)

https://developer.roblox.com/en-us/api-reference/function/WorldRoot/FindPartsInRegion3

Use this function to determine any parts in a given area (represented by a Region3 value), if those parts belong to a player’s character then prevent them from shooting.

so i can use this to check behind walls right?

The Region3 area behind the wall, yes.

wait nvm, i can use it to check if there are any walls where the player is aiming right?
i didnt write it correctly

you didn’t get him right. He wants to NOT shoot when enemy hide behind wall or else.

Yes, first he can check if a player’s parts are behind a wall in a Region3 area & if they are then he can disable their tools.

wait, but its npcs, and i want to cancel the local player’s tools if the enemies hide

Wait, how you make it not shoot when it already made shot?

Config.Ammo.Value -= 1
SFX.Shoot:Play()
Particles.Enabled = false
wait(2)

amma confused about this part

ima add a wait(2) that i forgot to add before shooting, bc of the aim cooldown, so if the enemy hides before the wait(2) there will be the function that before shooting detect it, idk if this explained well

so basically the script becomes this

local tool = script.Parent
local remote = tool:WaitForChild("OnShoot")
local shoot_part = tool:WaitForChild("FirePart")
local Config = tool:WaitForChild("Config")
local Damage = Config.Damage.Value
local SFX = tool:WaitForChild("Handle")
local Particles = shoot_part:WaitForChild("Light", "Smoke")

local Shooting = false

remote.OnServerEvent:Connect(function(player, target)
	if target:IsA("BasePart") and target.Parent:FindFirstChild("ZombieNoid") then
		if not Shooting and Config.Ammo.Value > 0 then
-- ima add the wait(2) here
-- then check here
			Config.Ammo.Value -= 1
			SFX.Shoot:Play()
			Particles.Enabled = false
			wait(2)
		end
	end
end)

i realised that i made it more confusing, the detect thing doesnt comes after particles.enabled and yes before config.ammo

Still don’t get it, but i’ll write you raycast thing, you will put it anywhere you need:

local filter = {player.Character}
for i,v in pairs(workspace:GetChildren()) do
	if v:FindFirstChild("ZombieNoid") then
		table.insert(filter, #filter+1, v)
	end
end --We getting all current enemies, as you said its ZombieNoid, so i changed it.

local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Blacklist
params.FilterDescendantsInstances = filter --Parameters for raycast, i put it so it ignores players and enemies itself

local raycast = workspace:Raycast(player.Character.HumanoidRootPart.Position, (target.Position-player.Character.HumanoidRootPart.Position).unit*1000, params) --Making the raycast, shooting it from player's center to zombie's center.

if raycast then --If this ray hits something, then there is a barrier/wall/door/tree/anything between both guys.
	--Enemy behind the wall.
else --No hits, you can shoot.
	
end

If i got it correctly you need to put it before your wait(2) or smth, you got me.

But this all script into your shoot thing, entire thing before wait(2)

1 Like

but wait, could u explain me what the parts of the scripts does? so i can implement it into the system? also i cant use Humanoid i need to use ZombieNoid, bc of the firing local scrpt

1 Like

I will edit it rn, so you will see explanations of everything.

ok thx!

message has no 30 letters

I saw ur edit, and it its just what im looking for! 100% sure that this is gonna work!
but, can u tell me how do i make it so it only starts shooting if the player is in a certain range

--Changed this line:
if not Shooting and Config.Ammo.Value > 0 and (player.Character.HumanoidRootPart.Position-target.Position).magnitude <= 250 then --250 is number you can change, thats distance. 

oh ima try that later cuz im not at pc right now, thx!

1 Like

everything worked as intended, thx!

1 Like