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

How can I make the Classic Roblox Slingshot not team-kill? [closed]

Asked by 5 years ago

I am stumped. I played the slingshot with a kills leaderboard and it doesn't register kills. That is not what I am worried about but it probably ties in with my problem. You are able to kill members of your team with the slingshot and I want to stop it. I tried making the pellet color your team color and if it touches a player with the same team color, it won't do damage. It didn't work at all and I discarded it. For reference, here are the relevant slingshot scripts:

Pellet Script:

local debris = game:service("Debris")
pellet = script.Parent
damage = 8

function onTouched(hit)
    if not hit or not hit.Parent then return end
    local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
    if humanoid then
        tagHumanoid(humanoid)
        humanoid:TakeDamage(damage)
    else
        damage = damage / 2
        if damage < 1 then
            connection:Disconnect()
            pellet.Parent = nil
        end
    end
end

function tagHumanoid(humanoid)
    -- todo: make tag expire
    local tag = pellet:FindFirstChild("creator")
    if tag then
        -- kill all other tags
        while(humanoid:FindFirstChild("creator")) do
            humanoid:findFirstChild("creator").Parent = nil
        end

        local new_tag = tag:Clone()
        new_tag.Parent = humanoid
        debris:AddItem(new_tag, 1)
    end
end

connection = pellet.Touched:Connect(onTouched)

r = game:service("RunService")
t, s = r.Stepped:Wait()
d = t + 2.0 - s
while t < d do
    t = r.Stepped:Wait()
end

pellet:Destroy()

Slingshot Script:

Pellet.LeftSurface = 0
Pellet.RightSurface = 0
Pellet.TopSurface = 0
Pellet.Shape = 0
Pellet.Size = Vector3.new(1,1,1)
Pellet.BrickColor = BrickColor.new(21)
script.Parent.PelletScript:Clone().Parent = Pellet

function fire(mouse_pos)


    Tool.Handle.SlingshotSound:Play()

-- find player's head pos

    local vCharacter = Tool.Parent
    local vPlayer = game.Players:GetPlayerFromCharacter(vCharacter)

    local head = vCharacter:FindFirstChild("Head")
    if not head then return end

    local dir = mouse_pos - head.Position
    dir = computeDirection(dir)

    local launch = head.Position + 5 * dir

    local delta = mouse_pos - launch

    local dy = delta.y

    local new_delta = Vector3.new(delta.x, 0, delta.z)
    delta = new_delta

    local dx = delta.magnitude
    local unit_delta = delta.unit

    -- acceleration due to gravity in RBX units
    local g = (-9.81 * 20)

    local theta = computeLaunchAngle( dx, dy, g)

    local vy = math.sin(theta)
    local xz = math.cos(theta)
    local vx = unit_delta.x * xz
    local vz = unit_delta.z * xz


    local missile = Pellet:Clone()




    missile.Position = launch
    missile.Velocity = Vector3.new(vx,vy,vz) * VELOCITY

    missile.PelletScript.Disabled = false

    local creator_tag = Instance.new("ObjectValue")
    creator_tag.Value = vPlayer
    creator_tag.Name = "creator"
    creator_tag.Parent = missile

    missile.Parent = workspace

end


function computeLaunchAngle(dx,dy,grav)
    -- arcane
    -- http://en.wikipedia.org/wiki/Trajectory_of_a_projectile

    local g = math.abs(grav)
    local inRoot = (VELOCITY*VELOCITY*VELOCITY*VELOCITY) - (g * ((g*dx*dx) + (2*dy*VELOCITY*VELOCITY)))
    if inRoot <= 0 then
        return .25 * math.pi
    end
    local root = math.sqrt(inRoot)
    local inATan1 = ((VELOCITY*VELOCITY) + root) / (g*dx)

    local inATan2 = ((VELOCITY*VELOCITY) - root) / (g*dx)
    local answer1 = math.atan(inATan1)
    local answer2 = math.atan(inATan2)
    if answer1 < answer2 then return answer1 end
    return answer2
end

function computeDirection(vec)
    local lenSquared = vec.magnitude * vec.magnitude
    local invSqrt = 1 / math.sqrt(lenSquared)
    return Vector3.new(vec.x * invSqrt, vec.y * invSqrt, vec.z * invSqrt)
end




Tool.Enabled = true
function onActivated()

    if not Tool.Enabled then
        return
    end

    Tool.Enabled = false

    local character = Tool.Parent;
    local humanoid = character.Humanoid
    if not humanoid  then
        print("Humanoid not found")
        return 
    end

    local targetPos = MouseLoc:InvokeClient(game:GetService("Players"):GetPlayerFromCharacter(character))

    fire(targetPos)

    wait(.2)

    Tool.Enabled = true
end

script.Parent.Activated:Connect(onActivated)

Client Script:

local Tool = script.Parent

local MouseLoc = Tool:WaitForChild("MouseLoc")

function MouseLoc.OnClientInvoke()
    return game:GetService("Players").LocalPlayer:GetMouse().Hit.p
end
0
The community guidelines of this website clearly forbid this kind of question: "Attempts should be your own work. Don't take a free model and expect us to fix it." User#25115 0 — 5y

Closed as Not Constructive by Gey4Jesus69, DinozCreates, and WideSteal321

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?