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

Weapon damage on touched?

Asked by
I_0KI 40
6 years ago
Edited 6 years ago

I have a script that makes an orange (Like the fruit) weapon and I want to keep it a localscript. But I am having issues with doing damage. I want to have it so it does damage to anybody that touches it except the localplayer. But it does no damage at all. Could someone help me with this? And sorry if this is a really dumb question I'm a scripting noob.

Orange = Instance.new("Tool",game.Players.LocalPlayer.Backpack)
OrangeModel = Instance.new("Part",Orange)
OrangeModel.Shape = "Ball"
local player = "LocalPlayer"
playername = game.Players[player].Name
lpchar = game.Workspace[playername]

--Top
TopDecal = Instance.new("Decal",OrangeModel)
TopDecal.Texture = "http://www.roblox.com/asset/?id=4701252"
TopDecal.Face = "Top"
--Back
TopDecal = Instance.new("Decal",OrangeModel)
TopDecal.Texture = "http://www.roblox.com/asset/?id=4701252"
TopDecal.Face = "Back"
--Bottom
TopDecal = Instance.new("Decal",OrangeModel)
TopDecal.Texture = "http://www.roblox.com/asset/?id=4701252"
TopDecal.Face = "Bottom"
--Left
TopDecal = Instance.new("Decal",OrangeModel)
TopDecal.Texture = "http://www.roblox.com/asset/?id=4701252"
TopDecal.Face = "Left"
--Front
TopDecal = Instance.new("Decal",OrangeModel)
TopDecal.Texture = "http://www.roblox.com/asset/?id=4701252"
TopDecal.Face = "Front"
--Right
TopDecal = Instance.new("Decal",OrangeModel)
TopDecal.Texture = "http://www.roblox.com/asset/?id=4701252"
TopDecal.Face = "Right"

--Other
Orange.Name = "Orange"
OrangeModel.Name = "Handle"

--Size
Vector = Vector3.new(1,1,1)
OrangeModel.Size = Vector

--Damage Where it won't work
OrangeModel.Touched:connect(function(p)

if not p.Parent.Name == lpname then
p.Parent.Humanoid.Health = 0

end
end)

local tool = game.Players.LocalPlayer.Backpack.Orange
local animation = Instance.new("Animation")
local player = "LocalPlayer"

local function onActivate()
animation.AnimationId = "http://www.roblox.com/Asset?ID=28090109"
local animTrack = game.Players[player].Character.Humanoid:LoadAnimation(animation)
animTrack:Play()
end

tool.Activated:connect(onActivate)

0
Im not an expert but I do believe the ``if not`` part is the problem. Generally the scripts where I used ``if not`` did not function very well or at all. So trying taking the ``If not`` out  and do try something else Audiimo 105 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

The player has a reference which allows you to accurately find the player. I discovered two reasons why your script wasn't working. 1) You tried finding the player's character before it existed. 2) You tried using a variable that wasn't defined.

I did some modifications, so you should be fine now.

local Orange = Instance.new("Tool",game.Players.LocalPlayer.Backpack)
local OrangeModel = Instance.new("Part",Orange)
OrangeModel.Shape = "Ball"
local player = game.Players.LocalPlayer
lpname = player.Name
repeat wait() until player.Character
lpchar = player.Character

--Top
TopDecal = Instance.new("Decal",OrangeModel)
TopDecal.Texture = "http://www.roblox.com/asset/?id=4701252"
TopDecal.Face = "Top"
--Back
TopDecal = Instance.new("Decal",OrangeModel)
TopDecal.Texture = "http://www.roblox.com/asset/?id=4701252"
TopDecal.Face = "Back"
--Bottom
TopDecal = Instance.new("Decal",OrangeModel)
TopDecal.Texture = "http://www.roblox.com/asset/?id=4701252"
TopDecal.Face = "Bottom"
--Left
TopDecal = Instance.new("Decal",OrangeModel)
TopDecal.Texture = "http://www.roblox.com/asset/?id=4701252"
TopDecal.Face = "Left"
--Front
TopDecal = Instance.new("Decal",OrangeModel)
TopDecal.Texture = "http://www.roblox.com/asset/?id=4701252"
TopDecal.Face = "Front"
--Right
TopDecal = Instance.new("Decal",OrangeModel)
TopDecal.Texture = "http://www.roblox.com/asset/?id=4701252"
TopDecal.Face = "Right"

--Other
Orange.Name = "Orange"
OrangeModel.Name = "Handle"

--Size
OrangeModel.Size = Vector3.new(1,1,1)

--Damage
OrangeModel.Touched:connect(function(p)
    if p.Parent:FindFirstChild("Humanoid") then 
        if p.Parent.Name ~= lpname then
            p.Parent.Humanoid.Health = 0
        end
    end
end)

local animation = Instance.new("Animation")

local function onActivate()
animation.AnimationId = "http://www.roblox.com/Asset?ID=28090109"
local animTrack = player.Character.Humanoid:LoadAnimation(animation)
animTrack:Play()
end

Orange.Activated:connect(onActivate)

As you can see, the local player is much easier to use than what you had. Also, your damage script did not check if the touching part was a player/NPC, so it could easily break. You used so many useless scripting techniques it is crazy. Try keep your scripts simpler.

Thanks,

Explosion

Ad

Answer this question