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

How do I make these blocks do Damage?

Asked by 9 years ago

I made a function where when you press f 8 red bullets are shot from your character(with fire on them) I want them to do damage but I have no idea how, can anyone help? Also, this only works in Studio, not on a server. When I try to play it on a server is says " Players.Player1.Backpack.LocalScript:5: attempt to index field 'Character' (a nil value)"

Player = game.Players.LocalPlayer

Char = Player.Character

Torso = Player.Character.Torso

Mouse = Player:GetMouse()

Attack = false

Mouse.KeyDown:connect(function(key)
    local key  = key:lower()
    if key == "f" and (not Attack) then
        Attack = true
       for i = 0, 8, 1 do
            local Ball = Instance.new("Part")
            Ball.Name = "Bullets"
            Ball.Parent = game.Workspace
            Ball.Shape = "Ball"
            Ball.Size = Vector3.new(1,1,1)
            Ball.Transparency = .4
            Ball.TopSurface = "Smooth"
            Ball.BottomSurface = "Smooth"
            Ball.BrickColor = BrickColor.new("Really red")
            Ball.CFrame = Torso.CFrame * CFrame.new(0,0.5, -5)
            Ball.CanCollide = false
            local Fire = Instance.new("Fire")
            Fire.Parent = Ball
            local Bv = Instance.new("BodyVelocity")
            Bv.Parent = Ball
            Bv.maxForce = Vector3.new(math.huge,math.huge,math.huge)
            Bv.velocity = Torso.CFrame.lookVector * 100   
            wait(.05)
       end
       wait(4)
       for _, v in pairs(game.Workspace:GetChildren()) do
         if v.Name == "Bullets" then
          v:Destroy()
          end
       end
    wait(1)
    Attack = false
    end
end)

2 answers

Log in to vote
0
Answered by
yumtaste 476 Moderation Voter
9 years ago

EDIT: Sorry about the link. That was for instakill, I thought it would be TakeDamage(100). Anyways, you would do humanoid:TakeDamage(damage) to hurt players. I used pseudocode for this, so you need to define humanoid and you can replace damage with a number, or create a variable named "damage" and set its value to a number.

0
Thanks, but his code will differ because he wants to damage, not instakill. DrJonJ 110 — 9y
0
He should know how to change the damage variable. I haven't thoroughly checked the blog post, I'll do that now. yumtaste 476 — 9y
0
I just updated the post to include taking damage for lava bricks, in the sense of a time delay for additional damage to the player. I'll get into how I did that for the newer guys in a future post. DrJonJ 110 — 9y
Ad
Log in to vote
0
Answered by
DrJonJ 110
9 years ago

First off, it doesn't work in a server because the script will load before the character does.

Add this before defining the character

repeat
    wait() until Player.Character

Second, you need to add a .Touched event. As YumTaste mentioned, you can refer to my blog post about this similar topic, but when you get the the BreakJoints(), replace it with the following:

local hum = plr.Character:FindFirstChild('Humanoid')
if hum then
    hum:TakeDamage(20)
end

You can change the 20 to whatever you want the damage to be.

Edit: Here

Player = game.Players.LocalPlayer;
Char = Player.Character;
Torso = Player.Character.Torso;
Mouse = Player:GetMouse();
Attack = false;

Mouse.KeyDown:connect(function(key)
    local key  = key:lower()
    if key == "f" and (not Attack) then
        Attack = true
        local bullets = { }
        for i = 0, 8, 1 do
        local Ball = Instance.new("Part")
        Ball.Name = "Bullets"
        Ball.Parent = game.Workspace
        Ball.Shape = "Ball"
        Ball.Size = Vector3.new(1,1,1)
        Ball.Transparency = .4
        Ball.TopSurface = "Smooth"
        Ball.BottomSurface = "Smooth"
        Ball.BrickColor = BrickColor.new("Really red")
        Ball.CFrame = Torso.CFrame * CFrame.new(0,0.5, -5)
        Ball.CanCollide = false
        local Fire = Instance.new("Fire")
        Fire.Parent = Ball
        local Bv = Instance.new("BodyVelocity")
        Bv.Parent = Ball
        Bv.maxForce = Vector3.new(math.huge,math.huge,math.huge)
        Bv.velocity = Torso.CFrame.lookVector * 100   
        wait(.05)
        table.insert(bullets, ball)
        ball.Touched:connect(function(part)
            if part then
            local plr = game.Players:GetPlayerFromCharacter(part.Parent)
            if plr then
                local hum = plr.Character:FindFirstChild('Humanoid')
                if hum then
                    hum:TakeDamage(20)
                    ball:Remove()
                end
            end
        end)
    end
    wait(4)
    for _, v in pairs(bullets) do
        v:Destroy()
    end
    wait(1)
    Attack = false
    end
end)

That should work. Let me know if it doesn't.

0
Is this how I would do it? LordZerefu 30 — 9y
0
game.Workspace.Bullets.Touched(function(plr) local hum = plr.Character:FindFirstChild('Humanoid') if hum then hum:TakeDamage(20) end end) LordZerefu 30 — 9y
0
You should be adding the .Touched function to the code inline. Otherwise you have no way to reference the bullet. DrJonJ 110 — 9y

Answer this question