So today, i’m making a MILSIM game and i decided to take one of the gun systems in the toolbox but there is a problem, it team kills. My game is based off REALISTIC WAR by FractalReality, except with a different concept and different backstory.
Why cant i do it myself ?
I am still learning scripting, 3 months of learning LUA still doesn’t understand about Anti-Team kill codes. So thats why i look into free model codes and try understanding them
Please reply if you know, scripts are down below
ServerMain :
local maxammo = script.Parent.MaxAmmo.Value
local ammo = script.Parent.Ammo
local reloading = false
local spread = script.Parent.EDIT.Spread.Value
script.Parent.Shoot.OnServerEvent:Connect(function (plr,Target)
if plr.Character ~= nil and plr.Character.Humanoid.Health >= .1 and script.Parent.Enabled == true and reloading == false then
script.Parent.Enabled = false
local hit = false
if ammo.Value >= 1 then
ammo.Value = ammo.Value - 1
script.Parent.Handle.FireSound:Play()
for i = 1, script.Parent.EDIT.BulletsPerShot.Value do
local dist = (script.Parent.RPart.Position - Target).Magnitude
local ray = Ray.new(script.Parent.RPart.CFrame.p, (Target - script.Parent.RPart.CFrame.p + (Vector3.new(math.random(-spread,spread)/1000,math.random(-spread,spread)/1000,math.random(-spread,spread)/1000) * dist)).unit * 2000)
local part, position = workspace:FindPartOnRay(ray, plr.Character, false, true)
print(position)
print(part)
print(Target)
local beam = Instance.new("Part")
beam.Anchored = true
beam.CanCollide = false
beam.Material = "Neon"
if dist >= 60 then
beam.Size = Vector3.new(.05,.05,60)
else
beam.Size = Vector3.new(.05,.05,dist)
end
beam.CFrame = CFrame.new(script.Parent.RPart.CFrame.p, position) * CFrame.new(0,0,-beam.Size.Z/2)
game.Debris:AddItem(beam,10)
local fx = script.Parent.FX:Clone()
fx.Dist.Value = dist
fx.Parent = beam
fx.Disabled = false
beam.Parent = plr.Character
script.Parent.RPart.MuzzleFlashMain.Enabled = true
script.Parent.RPart.OverHeat.Enabled = true
script.Parent.RPart.Smoke.Enabled = true
script.Parent.RPart.Sparks.Enabled = true
script.Parent.RPart.Flash.Enabled = true
if part ~= nil then
if part.Parent:FindFirstChild("Humanoid") then
part.Parent:FindFirstChild("Humanoid"):TakeDamage(script.Parent.EDIT.Damage.Value)
hit = true
local blood = script.Parent.EDIT.Blood:Clone()
blood.Parent = part
blood.Enabled = true
game.Debris:AddItem(blood,.2)
elseif part.Parent.Parent:FindFirstChild("Humanoid") then
part.Parent.Parent:FindFirstChild("Humanoid"):TakeDamage(script.Parent.EDIT.Damage.Value)
hit = true
local blood = script.Parent.EDIT.Blood:Clone()
blood.Parent = part
blood.Enabled = true
game.Debris:AddItem(blood,.2)
end
end
local hole = Instance.new("Part")
hole.Size = Vector3.new(.2,.2,.2)
hole.Material = "Neon"
hole.Anchored = true
hole.BrickColor = BrickColor.new("Black")
hole.Shape = 0
hole.CanCollide = false
hole.Position = position
local dust = script.Parent.EDIT.Dust:Clone()
dust.Parent = hole
dust.Enabled = true
dust.Script.Disabled = false
game.Debris:AddItem(hole, 1)
if hit == false then
hole.Parent = workspace
end
end
local shell = Instance.new("Part")
shell.Size = Vector3.new(.5,.5,.1)
shell.Position = script.Parent.ShellPart.Position
shell.Orientation = script.Parent.ShellPart.Orientation
shell.CanCollide = false
local shellmesh = Instance.new("SpecialMesh",shell)
shellmesh.MeshId = "rbxassetid://94295100"
shellmesh.TextureId = "rbxassetid://94287792"
shellmesh.Scale = Vector3.new(3,3,1)
game.Debris:AddItem(shell,5)
local v = script.Parent.ShellPart.CFrame:vectorToWorldSpace(Vector3.new(2 + math.random(4,14),math.random(-2,2),math.random(-2,2)))
shell.Velocity = v
shell.RotVelocity = Vector3.new(math.random(-25,25)/12,math.random(-25,25)/12,math.random(-25,25)/12)
shell.Parent = plr.Character
wait()
script.Parent.RPart.MuzzleFlashMain.Enabled = false
script.Parent.RPart.OverHeat.Enabled = false
script.Parent.RPart.Smoke.Enabled = false
script.Parent.RPart.Sparks.Enabled = false
script.Parent.RPart.Flash.Enabled = false
elseif ammo.Value <= 0 then
script.Parent.Handle.NoAmmo:Play()
end
wait(script.Parent.EDIT.Firerate.Value)
script.Parent.Enabled = true
end
end)
script.Parent.Reload.OnServerEvent:Connect(function ()
if reloading == false then
reloading = true
script.Parent.Handle.Reload:Play()
wait(script.Parent.EDIT.ReloadTime.Value)
ammo.Value = maxammo
reloading = false
end
end)
ClientMain :
local held = false
local plr = game.Players.LocalPlayer
script.Parent.Equipped:Connect(function()
chr = script.Parent.Parent
end)
script.Parent.Activated:Connect(function ()
held = true
if script.Parent.Enabled == true then
script.Parent.Enabled = false
while held == true do
if script.Parent.EDIT.BurstShots.Value >= 2 then
for i = 1, script.Parent.EDIT.BurstShots.Value do
script.Parent.Shoot:FireServer(chr.Humanoid.TargetPoint)
fire:Play()
wait(script.Parent.EDIT.BurstRate.Value)
end
else
fire:Play()
script.Parent.Shoot:FireServer(chr.Humanoid.TargetPoint)
end
if script.Parent.EDIT.Auto.Value == false then
held = false
end
wait(script.Parent.EDIT.Firerate.Value + .1)
end
if script.Parent.EDIT.Auto.Value == false then
wait(script.Parent.EDIT.Firerate.Value + .1)
end
script.Parent.Enabled = true
end
end)
script.Parent.Deactivated:Connect(function()
held = false
end)
script.Parent.Equipped:Connect(function()
wait()
holdanim = chr.Humanoid:LoadAnimation(script.Parent.HoldAnim)
holdanim:Play()
rel = chr.Humanoid:LoadAnimation(script.Parent.ReloadAnim)
fire = chr.Humanoid:LoadAnimation(script.Parent.FireAnim)
gui = script.Ammo:Clone()
gui.Txt.Text = script.Parent.Ammo.Value
gui.Parent = plr.PlayerGui
on = true
end)
script.Parent.Unequipped:Connect(function()
held = false
holdanim:Stop()
fire:Stop()
rel:Stop()
gui:Destroy()
on = false
end)
game.Players.LocalPlayer:GetMouse().KeyDown:connect(function(KeyPressed)
if KeyPressed == "r" and script.Parent.Parent == plr.Character then
script.Parent.Reload:FireServer()
rel:Play()
end
end)
script.Parent:WaitForChild("Ammo").Changed:Connect(function ()
if on == true then
gui.Txt.Text = script.Parent.Ammo.Value
end
end)
Let me repeat myself, it is a free model and im planning on remaking the gun system soon. The free model is just for the Early Access of the game.