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)
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.
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.