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

Mouse.TargetFilter not working as it should?

Asked by 3 years ago
Edited 3 years ago

Hello guys! I'm trying to make a Battle Royale game, with guns. The problem is that I have invisible parts with CanCollide = false around it for a system to discover the regions. I grouped them all along with the storm in a model called "MapEnvironment". I set that model as mouse.TargetFilter, but the ray is still detecting collision with those parts. Any help? Thank you! Client Script:

repeat
    wait()
until game:IsLoaded()

local ts = game:GetService("TweenService")
local tool = script.Parent
local handle = tool:WaitForChild("Handle")
local effects = tool.Effects
local uis = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local char = plr.Character
local mouse = plr:GetMouse()
local gui = tool.GunInterface

local fire = false
local firing = false
local equip = false

local reloadtime = tool.ReloadTime.Value
local projectiles = tool.Projectiles.Value --number of bullets shot at once
local spread = tool.Spread.Value/100 --bullet spread
local damage = tool.Damage.Value/projectiles --hit damage
local firerate = tool.FireRate.Value
local cooldown = 1/firerate --fire delay
local reloading = false

mouse.TargetFilter = game.Workspace.MapEnvironment
gui.Background.WeaponName.Text = tool.Name
gui.Background.Magazine.Text = tool.Magazine.Value.."/"..tool.MagazineSize.Value
function newbullet()
    local x = math.random(-spread*100,spread*100)/100
    local y = math.random(-spread*100,spread*100)/100

    local direction = (CFrame.new(handle.CFrame.p,mouse.Hit.p)*CFrame.Angles(math.rad(x),math.rad(y),0)).lookVector
    local ray = Ray.new(effects.CFrame.p,(direction).unit*999)
    local part,pos = workspace:FindPartOnRay(ray,char)
    if part then
        if part.Parent.Name ~= plr.Name then
            tool:WaitForChild("Fire"):FireServer(part,pos,damage,tool.Magazine.Value,ray)
        end
    else
        tool:WaitForChild("Fire"):FireServer(part,pos,damage,tool.Magazine.Value,ray)
    end
end
local function getelim()
    local pa = game.Players.LocalPlayer.PlayerGui.ElimGui.Background
    local ti = TweenInfo.new(1)
    local go = {}
    go.Position = UDim2.new(0.25,0,0.08,0)
    pa.Position = UDim2.new(-0.5,0,0.08,0)
    local tw = ts:Create(pa,ti,go)
    tw:Play()
    wait(3)
    local go2 = {}
    go2.Position = UDim2.new(1,0,0.08,0)
    local tw2 = ts:Create(pa,ti,go2)
    tw2:Play()
end
local function reload()
    if equip == true and reloading == false and tool.Magazine.Value < tool.MagazineSize.Value then
        firing = false
        reloading = true
        fire = true
        tool.Reload:FireServer(reloadtime)
        wait(reloadtime)
        reloading = false
        fire = false
    end
end
tool.Magazine.Changed:Connect(function()
    gui.Background.Magazine.Text = tool.Magazine.Value.."/"..tool.MagazineSize.Value
end)
mouse.Button1Down:Connect(function()
    if equip then
        firing = true
    end
end)

mouse.Button1Up:Connect(function()
    firing = false
end)

tool.Equipped:Connect(function()
    if game.Players.LocalPlayer:WaitForChild("PlayerValues"):WaitForChild("Spanish").Value == false then
        if tool.Rarity.Value == "Común" then
            tool.Rarity.Value = "Common"
        end
        if tool.Rarity.Value == "Poco común" then
            tool.Rarity.Value = "Uncommon"
        end
        if tool.Rarity.Value == "Raro" then
            tool.Rarity.Value = "Rare"
        end
        if tool.Rarity.Value == "Épico" then
            tool.Rarity.Value = "Epic"
        end
        if tool.Rarity.Value == "Legendario" then
            tool.Rarity.Value = "Legendary"
        end
        if tool.Rarity.Value == "Mítico" then
            tool.Rarity.Value = "Mythic"
        end
        gui.Controls.Drop.Text = "V - Drop"
        gui.Controls.Reload.Text = "R - Reload"
        gui.Controls.ScopeIn.Text = "Right click - Scope in"
        gui.Stats.Damage.Text = "Damage: ".. tool.Damage.Value
        gui.Stats.FireRate.Text = "Fire rate: ".. tool.FireRate.Value
        gui.Stats.HeadshotMultiplier.Text = "Headshot multiplier: ".. tool.HeadshotMultiplier.Value
        gui.Stats.MagazineSize.Text = "Magazine size: ".. tool.MagazineSize.Value
        gui.Stats.Rarity.Text = tool.Rarity.Value
        gui.Stats.ReloadTime.Text = "Reload time: ".. tool.ReloadTime.Value
    else
        if tool.Rarity.Value == "Common" then
            tool.Rarity.Value = "Común"
        end
        if tool.Rarity.Value == "Uncommon" then
            tool.Rarity.Value = "Poco común"
        end
        if tool.Rarity.Value == "Rare" then
            tool.Rarity.Value = "Raro"
        end
        if tool.Rarity.Value == "Epic" then
            tool.Rarity.Value = "Épico"
        end
        if tool.Rarity.Value == "Legendary" then
            tool.Rarity.Value = "Legendario"
        end
        if tool.Rarity.Value == "Mythic" then
            tool.Rarity.Value = "Mítico"
        end
        gui.Controls.Drop.Text = "V - Soltar"
        gui.Controls.Reload.Text = "R - Recargar"
        gui.Controls.ScopeIn.Text = "Click derecho - Apuntar"
        gui.Stats.Damage.Text = "Daño: ".. tool.Damage.Value
        gui.Stats.FireRate.Text = "Cadencia: ".. tool.FireRate.Value
        gui.Stats.HeadshotMultiplier.Text = "Multiplicador cabeza: ".. tool.HeadshotMultiplier.Value
        gui.Stats.MagazineSize.Text = "Tamaño cargador: ".. tool.MagazineSize.Value
        gui.Stats.Rarity.Text = tool.Rarity.Value
        gui.Stats.ReloadTime.Text = "Tiempo recarga: ".. tool.ReloadTime.Value
    end
    equip = true
    gui.Parent = game.Players.LocalPlayer.PlayerGui
    mouse.Icon = "http://www.roblox.com/asset/?id=7074439897"
end)

tool.Unequipped:Connect(function()
    equip = false
    firing = false
    gui.Parent = tool
    mouse.Icon = "rbxasset://SystemCursors/Arrow"
end)
uis.InputBegan:Connect(function(key)
    if key.KeyCode == Enum.KeyCode.R and equip == true then
        reload()
    end
end)
tool.Fire.OnClientEvent:Connect(function(plr)
    game.Players.LocalPlayer.PlayerGui.ElimGui.Background.ElimName.Text = tool.ElimName.Value
    game.Workspace.ElimSound:Play()
    getelim()
end)
uis.InputBegan:Connect(function(key)
    if key.KeyCode == Enum.KeyCode.V then
        if equip then
            tool.Drop:FireServer(plr)
        end
    end
end)
tool.Headshot.OnClientEvent:Connect(function(plr)
    game.Workspace.HeadshotSound:Play()
end)
tool.BreakShield.OnClientEvent:Connect(function(plr)
    game.Workspace.BreakShield:Play()
end)
tool.DamageEvent.OnClientEvent:Connect(function(damage,victim,Shields)
    local ti = TweenInfo.new(
        1,
        Enum.EasingStyle.Sine,
        Enum.EasingDirection.Out,
        0,
        false,
        0
    )
    local go1 = {}
    go1.StudsOffsetWorldSpace = Vector3.new(2,5,0)
    local go2 = {}
    go2.TextTransparency = 1
    go2.TextStrokeTransparency = 1
    local bbg = Instance.new("BillboardGui")
    bbg.Size = UDim2.new(10,0,10,0)
    bbg.AlwaysOnTop = true
    bbg.StudsOffsetWorldSpace = Vector3.new(2,0,0)
    bbg.LightInfluence = 0
    bbg.Parent = victim
    if bbg.Parent.Name ~= "Head" then
        bbg.Parent = victim.Head
    end
    local tlbl = Instance.new("TextLabel")
    tlbl.Size = UDim2.new(1,0,1,0)
    tlbl.BackgroundTransparency = 1
    tlbl.TextScaled = true
    tlbl.Font = Enum.Font.Jura
    tlbl.TextStrokeTransparency = 0
    if Shields >= 1 then
        tlbl.TextColor3 = Color3.fromRGB(0,200,255)
        tlbl.TextStrokeColor3 = Color3.fromRGB(0,0,0)
    else
        tlbl.TextColor3 = Color3.fromRGB(255,0,0)
        tlbl.TextStrokeColor3 = Color3.fromRGB(255,255,255)
    end
    tlbl.Text = "-"..math.round(damage)
    tlbl.Parent = bbg
    ts:Create(bbg,ti,go1):Play()
    ts:Create(tlbl,ti,go2):Play()
end)
while true do
    if firing == true then
        if tool.Magazine.Value ~= 0 and equip and not reloading == true then
            if fire == false then
                reloading = false
                fire = true
                for i = 1,projectiles do
                    newbullet()
                end
                wait(0.1)
                wait(cooldown-0.1)
                fire = false
            end
        else
            reload()
        end
    end
    if firing == false then
        wait()
    end
end

Server Script (part of the fire event):

local tool = script.Parent
local fsize = 4
local handle = tool:WaitForChild("Handle")
local damagereceived = 0
local dmgind = false
local frate = (1/tool.FireRate.Value)/3
local tw = game:GetService("TweenService")
local pa = tool.Effects.Flare
local gui = tool.GunInterface
local function makerayvisible(ray)
    local ti = TweenInfo.new(1)
    local go = {}
    go.Transparency = 1

    local radius = 0.25
    local midpoint = ray.Origin + ray.Direction/2
    local raypart = Instance.new("Part")
    local ts = tw:Create(raypart,ti,go)
    raypart.Parent = tool

    raypart.Anchored = true
    raypart.CanCollide = false
    raypart.CanTouch = false
    raypart.CanQuery = false
    raypart.CFrame = CFrame.new(midpoint,ray.Origin)
    raypart.Size = Vector3.new(radius,radius,ray.Direction.magnitude)
    raypart.BackSurface = Enum.SurfaceType.Smooth
    raypart.FrontSurface = Enum.SurfaceType.Smooth
    raypart.TopSurface = Enum.SurfaceType.Smooth
    raypart.BottomSurface = Enum.SurfaceType.Smooth
    raypart.LeftSurface = Enum.SurfaceType.Smooth
    raypart.RightSurface = Enum.SurfaceType.Smooth

    raypart.Material = Enum.Material.Plastic
    raypart.Color = Color3.fromRGB(255,255,0)
    raypart.Name = "RayPart"
    game.Debris:AddItem(raypart,1)
    ts:Play()
    return raypart
end
tool:WaitForChild("Fire").OnServerEvent:Connect(function(plr,part,pos,damage,magazine,ray)
    handle.FireSound:Play()
    tool.Magazine.Value = magazine - 1
    local ti = TweenInfo.new(0.1)
    local go = {}
    go.Size = UDim2.new(fsize,0,fsize,0)
    local ts1 = tw:Create(pa,ti,go)
    ts1:Play()
    if part then
        if part.Parent and part.Parent:FindFirstChild("Humanoid") and part.Parent.Humanoid.Health > 0 or part.Parent.Parent:FindFirstChild("Humanoid") then
            if part.Name == "Head" or part.Name == "Handle" and humanoid.Health > 0 then

                        humanoid:TakeDamage(damage)
end
end)

1 answer

Log in to vote
0
Answered by
TGazza 1336 Moderation Voter
3 years ago

The Ray object can't be filtered (as far as i know), but there is another method/object named Raycast.

In your function named function newbullet() change the following:

(Note: the way this will work is substituting ray with raycast. They are similar but RayCast doesnt have some things Ray has and vice versa! So you will need to check and/or change any server/client scripts that are linked to this ray system. Noteworthy your Remote Event named "Fire".)

function newbullet()
    local x = math.random(-spread*100,spread*100)/100
    local y = math.random(-spread*100,spread*100)/100

    local direction = (CFrame.new(handle.CFrame.p,mouse.Hit.p)*CFrame.Angles(math.rad(x),math.rad(y),0)).lookVector
    -- Raycast Params!
    local raycastParams = RaycastParams.new()
    raycastParams.FilterDescendantsInstances = {game.Workspace.MapEnvironment,char}
    raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

    -- Might need to check/change this raycast Code as it was just a copy/paste from what you had!

    local myRayCast = workspace:Raycast(effects.CFrame.p,(direction).unit*999, raycastParams)
    --[[
    local ray = Ray.new(effects.CFrame.p,(direction).unit*999)
    local part,pos = workspace:FindPartOnRay(ray,char)
    ]]

    local part,pos = ray.Instance,ray.Instance.Position

    if part then
        if part.Parent.Name ~= plr.Name then
            tool:WaitForChild("Fire"):FireServer(part,pos,damage,tool.Magazine.Value,myRayCast) -- The script(s) listening to this Fire Event will need to be changed!
        end
    else
        tool:WaitForChild("Fire"):FireServer(part,pos,damage,tool.Magazine.Value,myRayCast) -- The script(s) listening to this Fire Event will need to be changed!
    end
end

Hope this helps!

Feel free to browse the wiki references below that i've included!

Wiki References: Ray Raycast

0
I tried it and the local script works well. The problem is that for any reason, "pos" is nil in the Server Script after the Local Script sent the "pos" (which isn't nll in that script) through Fire RemoteEvent. I edited the question adding the original Server Script, like if I didn't modify it. Robloximario952 18 — 3y
Ad

Answer this question