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)
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.
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)
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)
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
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.