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

Paintball gun kills players on the same team?

Asked by 2 years ago
Edited 2 years ago

I was working on a paintball game until i found out that the gun kills people on the same team which is not what i want

ball = script.Parent
damage = 100

function onTouched(hit)
    local humanoid = hit.Parent:findFirstChild("Humanoid")
    local playername = humanoid.Parent.Name
    local playerteam = game.Players['playername'].Team.Team


    if hit:getMass() < 1.2 * 200 then

    end
    -- make a splat
    for i=1,3 do
        local s = Instance.new("Part")
        s.Shape = 1 -- block
        s.formFactor = 2 -- plate
        s.Size = Vector3.new(1,.4,1)
        s.BrickColor = ball.BrickColor
        local v = Vector3.new(math.random(-1,1), math.random(0,1), math.random(-1,1))
        s.Velocity = 15 * v
        s.CFrame = CFrame.new(ball.Position + v, v)
        ball.BrickCleanup:clone().Parent = s
        s.BrickCleanup.Disabled = false
        s.Parent = game.Workspace
        --game.Debris:AddItem(s, 5)

    end


    if humanoid ~= nil and playerteam == 'Red' then
        tagHumanoid(humanoid)
        humanoid:TakeDamage(damage)
        wait(2)
        untagHumanoid(humanoid)
    end

    connection:Disconnect()
    ball.Parent = nil
end

function tagHumanoid(humanoid)
    -- todo: make tag expire
    local tag = ball:findFirstChild("creator")
    if tag ~= nil then
        local new_tag = tag:clone()
        new_tag.Parent = humanoid
    end
end


function untagHumanoid(humanoid)
    if humanoid ~= nil then
        local tag = humanoid:findFirstChild("creator")
        if tag ~= nil then
            tag.Parent = nil
        end
    end
end

connection = ball.Touched:connect(onTouched)

wait(8)
ball.Parent = nil

2 answers

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

You might wanna use the Teams service. Here's how:

local Teams = game:GetService("Teams")

local red = Teams.Red

function onTouched(hit)
    local humanoid = hit.Parent:findFirstChild("Humanoid")
    local playername = humanoid.Parent.Name
    local playerteam = game.Players['playername'].Team

if humanoid ~= nil and playerteam == red then
    tagHumanoid(humanoid)
    humanoid:TakeDamage(damage)
    wait(2)
    untagHumanoid(humanoid)
end

Hope this helps!

0
it gives this error 15:55:06.339 Workspace.Paintball.Paintball:9: attempt to index nil with 'Parent' - Server - Paintball:9 15:55:06.339 Stack Begin - Studio 15:55:06.339 Script 'Workspace.Paintball.Paintball', Line 9 - function onTouched - Studio - Paintball:9 15:55:06.339 Stack End - Studio basloxrob 9 — 2y
0
Maybe because you typed `findFIrstChild` instead of `FindFirstChild` at `Line 06` on the code above. Dehydrocapsaicin 483 — 2y
0
that's just a deprecated from of it and now it doesn't splat on hit anymore basloxrob 9 — 2y
0
also i added the whole script basloxrob 9 — 2y
Ad
Log in to vote
0
Answered by 2 years ago
Edited 2 years ago
[playername]*  

not

['playername']
local playerteam = game.Players[playername].Team.Team
0
it says Workspace.Paintball.Paintball:6: attempt to index nil with 'Parent' basloxrob 9 — 2y
0
local humanoid = hit.Parent:findFirstChild("Humanoid");if not humanoid then return end; scripterhelper5354 896 — 2y
0
it works but now it doesn't do damage or splat when it hits the ground basloxrob 9 — 2y
0
hello? basloxrob 9 — 2y

Answer this question