Still have questions? Join our Discord server and get real time help.
0

# How should I fix the laser damage from my code?

Hi guys, I'm doing my first game, basically it's a survival game, where you have machines that want to kill you, and you have to find a safe place. These machines are the loose lasers, two in each arm. The laser script has a problem, they were meant to deal damage, but they're not. They are lasers that are released in different directions. Here is the original script:

gun = script.Parent

function findNearestTorso(pos)
local list = game.Workspace:children()
local torso = nil
local dist = 450
local temp = nil
local human = nil
local temp2 = nil
for x = 1, #list do
temp2 = list[x]
if (temp2.className == "Model") and (temp2 ~= script.Parent) then
temp = temp2:findFirstChild("Torso")
human = temp2:findFirstChild("Humanoid")
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
if (temp.Position - pos).magnitude < dist then
torso = temp
dist = (temp.Position - pos).magnitude
end
end
end
end
end

while true do
wait(.5)
local target = findNearestTorso(script.Parent.Position)
if target ~= nil then
local P = Instance.new("Part")
P.Name = "Laser"
P.formFactor = 0
P.Size = Vector3.new(10,10,10)
connection = script.Parent.Touched:connect(onTouch)
local Distance = (target.Position - gun.Position).magnitude
script.Parent.Mesh.Scale = Vector3.new(0.25, 0.25, Distance)
P.CFrame = CFrame.new(gun.Position, target.Position)*CFrame.new(0, 0, -Distance/2)
script.Parent.Mesh:Clone().Parent = P
P.Parent = game.Workspace
P.BrickColor = BrickColor.new("Toothpaste")
P.Transparency = 0
P.Reflectance = 0
P.Material = "Neon"
P.Anchored = true
P.CanCollide = true
target.Parent.Humanoid.Health = target.Parent.Humanoid.Health - 100
wait(1)
P:remove()
end
end


Well the idea is that the laser HAVE TO HURT IN THE TIME YOU GET ON, but I still do not know anything about roblox lua, so I'm in need of help. The idea is to deal damage when it hits, But when I hit no harm, I hope you guys help me.

0
Try Replacing line 46L target.Parent.Humanoid.Health = target.Parent.Humanoid.Health = 0 ItzJester2 12 — 2mo
0
still not working =/ RenatoGameplays -2 — 2mo

0
Goulstem 7975
2 months ago

You are connecting a function onTouch on line 34 to the Touched event. But onTouch has not been defined.

You need to define it:

local dmg = 100; --Variable for damage
local db = false; --Debounce

function onTouch(hit) --'hit' is the object the laser has hit
if db then return end --Debounce logic
db = true;
--Check for player
local hum = hit.Parent:FindFirstChild("Humanoid");
if hum then
--Damage player
hum.Health = hum.Health - dmg;
end
db = false;
end

0
Define it before the while loop! Goulstem 7975 — 2mo
0
Oh my God, I knew it had something to do with it, thank you. But there is a problem, I do not know where to put this. Please can you get the original script and add your configuration? I really do not know anything about .lua yet RenatoGameplays -2 — 2mo
0
Before the while loop sir! :D Goulstem 7975 — 2mo