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

Help With 'Return'? (ANSWERED)

Asked by 9 years ago

So I have a script inside of a part called 'Glass'

Whenever something touches it with a Parent named 'Zombie' it takes away one from a value called 'Health' in the Part.

The script works fine, if the zombie touches it first. But if the player touches it first, then the zombie won't make any change to it.

I think I know how to fix this, but I'm not sure. I think I have to use 'else return' somewhere, but everywhere i tried to put it it didn't work.

This is the script inside the glass part:

local db = 0
local health = script.Parent:WaitForChild("Health")

function onTouched(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    local human = hit.Parent:findFirstChild("Humanoid")
    if db == 0 then
        db = 1
        if hit.Parent.Name == "Zombie" then
        health.Value = health.Value - 1
        print("ow")
        print(health.Value)
        wait(math.random(1,3))
        db = 0

        end
        end
    end


script.Parent.Touched:connect(onTouched)

2 answers

Log in to vote
2
Answered by 9 years ago

I don't think you need to use "return" for this script. I believe the problem occurs when you change the value of db.

if db == 0 then
    db = 1
    if hit.Parent.Name == "Zombie" then
        health.Value = health.Value - 1
        print("ow")
        print(health.Value)
        wait(math.random(1,3))
        db = 0
    end
end

Notice how regardless of who touches the block, db becomes 1. This means that non-zombies can change db's value to 1. However, only zombies can change the db value to 0! So when a non-zombie touches the part, it turns db to 1 and doesn't change it back. This causes the if statement to fail.

if db == 0 then
    if hit.Parent.Name == "Zombie" then
        db = 1
        health.Value = health.Value - 1
        print("ow")
        print(health.Value)
        wait(math.random(1,3))
        db = 0
    end
end

Simply move the line of code inside the if statement. Now non-zombies won't change the db value!

1
Man, I feel stupid. Well I guess that's what this website is for. It works too. Thanks so much! (Why is this post downvoted :I) SpazzMan502 133 — 9y
0
No problem! IcyArticunoX 355 — 9y
Ad
Log in to vote
0
Answered by
saenae 318 Moderation Voter
9 years ago

~~~~~~~~~~~~~~~~~ local db = 0 local health = script.Parent.Health

script.Parent.Touched:connect(function(h)
if db == 0 then if h.Parent.Name == "Zombie" then db = 1 health.Value = health.Value - 1 print("OW!") print(health.Value) wait(math.random(1, 3)) db = 0 end end end) ~~~~~~~~~~~~~~~

You can add back in your variables, but this functions regardless of what touches the block first. (Tested by making a clone of myself named 'zombie' touch the brick after I had.

0
Sorry if it's hard to read, unsure how to insert code format saenae 318 — 9y
0
That wasn't the problem, I've tested it and it worked, except for the problem above. SpazzMan502 133 — 9y

Answer this question