Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I add Friendly Fire to my guns?

Asked by 4 years ago

I'll oversimplify this. Alright so I have a local script which handles most of everything that includes shooting it. Although I'm having trouble adding friendly fire. does anybody know how to do it?

--Roblox Services--
local Players = game.Players

--//Variables\\--
local tool = script.Parent.Parent
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local hole = tool:WaitForChild("Hole")
local handle = tool:WaitForChild("Handle")
local actionService = game:GetService("ContextActionService")
local mouseDown = false
local equipped = false
local Configurations = tool.Configurations

--//Folders\\--
local animations = tool:WaitForChild("Animations")
local configs = tool:WaitForChild("Configurations")
local remotes = tool:WaitForChild("Remotes")
local bindables = tool:WaitForChild("Bindables")

--//Animations\\--
local recoilAnim = animations:WaitForChild("Recoil")
local reloadAnim = animations:WaitForChild("Reload")
local holdAnim = animations:WaitForChild("Hold")

--//Animation Tracks\\--
local recoilTrack
local reloadTrack
local holdTrack

--//Configs\\--
local allowTracing = configs:WaitForChild("AllowTracing")
local range = configs:WaitForChild("Range")

--//Remotes\\--
local canShoot = remotes:WaitForChild("CanShoot")
local canReload = remotes:WaitForChild("CanReload")
local hitRemote = remotes:WaitForChild("Hit")
local reloadRemote = remotes:WaitForChild("Reload")
local shootRemote = remotes:WaitForChild("Shoot")

--//Assets\\--
local flashGui = hole:WaitForChild("FlashGui")

--//Bindables\\--
local reloadBind = bindables:WaitForChild("Reload")

--//Functions\\--
local function equip()
    equipped = true

    --//Get humanoid
    local character = player.Character or player.CharacterAdded:Wait()
    local humanoid = character:WaitForChild("Humanoid")

    if humanoid then
        --//Load animations
        pcall(function()
            --//Hold animation
            holdTrack = humanoid:LoadAnimation(holdAnim)
            holdTrack:Play()
        end)

        pcall(function()
            --//Reload animation
            reloadTrack = humanoid:LoadAnimation(reloadAnim)
        end)

        pcall(function()
            --//Recoil animation
            recoilTrack = humanoid:LoadAnimation(recoilAnim)
        end)
    end
end

local function unequip()
    equipped = false

    --//Stop animations
    if holdTrack then
        holdTrack:Stop()
    end

    if reloadTrack then
        reloadTrack:Stop()
    end

    if recoilTrack then
        recoilTrack:Stop()
    end
end

local function reload()
    if canReload:InvokeServer() then
        --//Reload
        reloadRemote:FireServer()

        if reloadTrack then
            reloadTrack:Play()
        end
    end
end

local function fire()
    --//Checks
    if canShoot:InvokeServer() then
        --//Initialize
        if recoilTrack then
            recoilTrack:Play()
        end
        flashGui.Enabled = true

        --//Cast ray
        local ray = Ray.new(hole.CFrame.p, (mouse.hit.p - hole.CFrame.p).unit * range.Value)
        local touch, position = workspace:FindPartOnRay(ray, player.Character, false, true)

        --//Hit detection
        if touch then
            hitRemote:FireServer(touch)
        end
        shootRemote:FireServer()

        --//Trace
        if allowTracing.Value then
            --//Create
            local trace = Instance.new("Part")
            trace.Anchored = trace
            trace.CanCollide = false
            trace.Transparency = 0.5
            trace.BrickColor = BrickColor.new("Bright yellow")
            trace.Material = Enum.Material.SmoothPlastic

            --//Calculate
            local distance = (hole.CFrame.p - position).magnitude
            trace.Size = Vector3.new(0, 0, distance)
            trace.CFrame = CFrame.new(hole.CFrame.p, position) * CFrame.new(0, 0, -distance/2)
            trace.Parent = workspace

            --//Clean-up
            game:GetService("Debris"):AddItem(trace, 0.1)

            wait(0.1)

            flashGui.Enabled = false
        end
    end
end
0
You can make an if statement to check the hitted player's team. TheRealPotatoChips 793 — 4y
0
Find the player instance from the players character(using GetPlayerFromCharacter) then check which team the player is on. Compare that with the team of the player shooting(with an IF statement) ForeverBrown 356 — 4y

Answer this question