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

How do I fire a different Part out of a weapon depending on what team the player is on?

Asked by
JAG4UR 0
6 years ago

Hello. I use a gun system which I've modified to fire a Part copied from ReplicatedStorage which have premade Trail effects inside them. I'm trying to have it so that if you're on a certain team, a certain bullet will be fired so that blue team will fire blue tracers and red team will fire red tracers etc however only one of the team's bullets actually appear when fired.

script.Parent.Handle.Fire:Play()
local tc = game.Players.LocalPlayer.TeamColor

if tc == BrickColor.new("Bright blue") then 
    local Shot = game.ReplicatedStorage:FindFirstChild("BlueShot"):clone()

Firing only one type of bullet works perfectly fine however as I said as soon as I try to fire a different one only the second bullet actually appears

script.Parent.Handle.Fire:Play()
local tc = game.Players.LocalPlayer.TeamColor

if tc == BrickColor.new("Bright blue") then 
    local Shot = game.ReplicatedStorage:FindFirstChild("BlueShot"):clone()

else if tc == BrickColor.new("Really red") then 
    local Shot = game.ReplicatedStorage:FindFirstChild("RedShot"):clone()

Shots fired in blue team don't appear whilst shots fire in red team do appear. Incase it's important, here is the full Fire function of the gun

function Fire()

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")

local fir = script.Parent.FIRING
local firP = game.Players:GetPlayerFromCharacter(script.Parent.Parent).Character.Humanoid:LoadAnimation(fir)
firP:Play()

script.Parent.Handle.Fire:Play()
local tc = game.Players.LocalPlayer.TeamColor

if tc == BrickColor.new("Bright blue") then 
    local Shot = game.ReplicatedStorage:FindFirstChild("BlueShot"):clone()

else if tc == BrickColor.new("Really red") then 
    local Shot = game.ReplicatedStorage:FindFirstChild("RedShot"):clone()

  game:GetService("Debris"):AddItem(Shot, 1) -- add it to debris, just in case
  Shot.FormFactor = "Custom"
  Shot.Name = "Bullet"
  Shot.Size = Vector3.new(0.1,0.1,3)
  Shot.BrickColor = BulletColour
  Shot.TopSurface = "Smooth"
  Shot.BottomSurface = "Smooth"
  Shot.Transparency = 0
  Shot.Material = "Neon"
  Shot.Anchored = true
  Shot.CanCollide = false
    if not script.Parent:findFirstChild("Silencer") then
    local light = Instance.new("PointLight")
    light.Range = 5
    light.Color = OutlineColour.Color
    light.Brightness = 2
    light.Parent = Shot
    end

  local mesh = Instance.new("SpecialMesh")
  mesh.MeshType = "Brick"
  mesh.Parent = Shot
  Shot.Parent = workspace
  Direct = CFrame.new(script.Parent.Flash.Position , script.Parent.Parent.Humanoid.TargetPoint)
  TargetToChange = Direct + Direct.lookVector*1000
  OffsetPosition = TargetToChange.p + Vector3.new(math.random(-Accuracy,Accuracy),math.random(-Accuracy,Accuracy),math.random(-Accuracy,Accuracy))
  Shot.CFrame = CFrame.new(script.Parent.Flash.Position , OffsetPosition) -- phew, now that's alot of maths
  Shot.CFrame = Shot.CFrame + Shot.CFrame.lookVector*1.25
  mesh.Scale = Vector3.new(1,1,1)
  mesh.VertexColor = Vector3.new(BulletColour.r , BulletColour.g , BulletColour.b)

local selection = Instance.new("SelectionBox")
    selection.Parent = Shot
    selection.Color = OutlineColour
    selection.LineThickness = 0.05
    selection.Transparency = 0.5
    selection.Adornee = Shot
    selection.Visible = true

  MoveBullet(Shot)
end
end
end

1 answer

Log in to vote
0
Answered by
deris88 146
6 years ago

Hi.

Yes, you made a small mistake. It's not critical at all!

Where you used if tc == BrickColor.new("Bright blue") then at line 15 you did not put any kind of code under it, whereas under the else if tc == BrickColor.new("Really red") then you did put a code.

This is how you could fix it:

if tc == BrickColor.new("Bright blue") then
    local Shot = game.ReplicatedStorage:FindFirstChild("BlueShot"):clone()
else if tc == BrickColor.new("RedShot") then
    local Shot = game.ReplicatedStorage:FindFirstChild("RedShot"):clone()
end end

--Now that we defined the color of shot, we should call a function which will spawn the shot:

MakeShot() --Here we will spawn the desired shot.

--------------------------------------------------
--Here is the function that we have just called:
function MakeShot()
  game:GetService("Debris"):AddItem(Shot, 1) -- add it to debris, just in case
  Shot.FormFactor = "Custom"
  Shot.Name = "Bullet"
  Shot.Size = Vector3.new(0.1,0.1,3)
  Shot.BrickColor = BulletColour
  Shot.TopSurface = "Smooth"
  Shot.BottomSurface = "Smooth"
  Shot.Transparency = 0
  Shot.Material = "Neon"
  Shot.Anchored = true
  Shot.CanCollide = false
  if not script.Parent:findFirstChild("Silencer") then
    local light = Instance.new("PointLight")
    light.Range = 5
    light.Color = OutlineColour.Color
    light.Brightness = 2
    light.Parent = Shot
  end

  local mesh = Instance.new("SpecialMesh")
  mesh.MeshType = "Brick"
  mesh.Parent = Shot
  Shot.Parent = workspace
  Direct = CFrame.new(script.Parent.Flash.Position , script.Parent.Parent.Humanoid.TargetPoint)
  TargetToChange = Direct + Direct.lookVector*1000
  OffsetPosition = TargetToChange.p + Vector3.new(math.random(-Accuracy,Accuracy),math.random(-Accuracy,Accuracy),math.random(-Accuracy,Accuracy))
  Shot.CFrame = CFrame.new(script.Parent.Flash.Position , OffsetPosition) -- phew, now that's alot of maths
  Shot.CFrame = Shot.CFrame + Shot.CFrame.lookVector*1.25
  mesh.Scale = Vector3.new(1,1,1)
  mesh.VertexColor = Vector3.new(BulletColour.r , BulletColour.g , BulletColour.b)

local selection = Instance.new("SelectionBox")
    selection.Parent = Shot
    selection.Color = OutlineColour
    selection.LineThickness = 0.05
    selection.Transparency = 0.5
    selection.Adornee = Shot
    selection.Visible = true

  MoveBullet(Shot)
end

Hope this helps!

0
Hey thanks for your response. Forgive me for my lack of knowledge but I'm not sure on how to keep the defined color of the shot localised to the Fire function. JAG4UR 0 — 6y
0
You don't have to keep it. At line 1-5 we did everything. Now script will use only the defined color. deris88 146 — 6y
0
I have seperately defined the color but no matter where I put it all of the Shots in the Fire functions are Unknown globals. JAG4UR 0 — 6y
Ad

Answer this question