Still have questions? Join our Discord server and get real time help.
Log in to vote

How should I fix the laser damage from my code?

Asked by 5 months ago

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
    return torso

while true do
    local target = findNearestTorso(script.Parent.Position)
    if target ~= nil then
        local P ="Part") 
        P.Name = "Laser" 
        P.formFactor = 0 
        P.Size =,10,10) 
                connection = script.Parent.Touched:connect(onTouch)
                local Distance = (target.Position - gun.Position).magnitude
        script.Parent.Mesh.Scale =, 0.25, Distance)
        P.CFrame =, target.Position)*, 0, -Distance/2)
        script.Parent.Mesh:Clone().Parent = P
                P.Parent = game.Workspace 
                P.BrickColor ="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

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.

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

1 answer

Log in to vote
Answered by
Goulstem 8005 Badge of Merit Moderation Voter Administrator Community Moderator
5 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;
    db = false;
Define it before the while loop! Goulstem 8005 — 5mo
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 — 5mo
Before the while loop sir! :D Goulstem 8005 — 5mo

Answer this question