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

My function get called the first time, but never after, is there a way to fix this?

Asked by 6 years ago

The monsterExp function only gets called once. I assume it is because the monster variable gets emptied when to gameObject gets destroyed. Is there any way to work around it. Also, another question, can you make labels update "live" and not after character reset?

monster1 = game.Lighting.Zombies
monster1.Parent = game.Workspace

exp = 0
level = 1
expreq = level*25 + 100
gold = 100
game.StarterGui.StatsGui.Level.Text ="Level: "..level
game.StarterGui.StatsGui.Exp.Text = "Exp: "..exp.."/"..expreq
game.StarterGui.StatsGui.Gold.Text = "Gold: "..gold


monster = workspace.Zombies.Zombie.Zombie
function monsterExp()
    print('hej')
    if monster.Health <= 0 then
        exp = exp + 25
        print(exp)
        game.StarterGui.StatsGui.Exp.Text = "Exp: "..exp.."/"..expreq
        print(game.StarterGui.StatsGui.Exp.Text)
    end
end

monster.HealthChanged:connect(monsterExp)

0
Are you making it so when the monster dies, it rewards exp? awesomeipod 607 — 6y
0
Yes allecoo1 0 — 6y
0
use humanoid.died if the monster has humanoid if not you can add one AdventurousHeight 5 — 6y
0
is this a script or localscript? and where is the script located? 4PlayerGamingRoblox 38 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago

Replace your current function and connection with this:

monster = workspace.Zombies.Zombie.Zombie

function monsterExp()
    print('hej')
    --if monster.Health >= 0 then -- don't need this anymore
        exp = exp + 25
        print(exp)
        game.StarterGui.StatsGui.Exp.Text = "Exp: "..exp.."/"..expreq
        print(game.StarterGui.StatsGui.Exp.Text)
    --end
end

monster.Died:connect(monsterExp)
0
still getting the same problem, the first time the function works, but the second time it doesnt allecoo1 0 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago
    monster1 = game.Lighting.Zombies -- change to ServerStorage
monster1.Parent = game.Workspace

exp = 0
level = 1
expreq = level*25 + 100
gold = 100
game.StarterGui.StatsGui.Level.Text ="Level: "..level
game.StarterGui.StatsGui.Exp.Text = "Exp: "..exp.."/"..expreq
game.StarterGui.StatsGui.Gold.Text = "Gold: "..gold


monster = workspace.Zombies.Zombie.Zombie -- what the f***?
function monsterExp()
   print('hej')
   if monster:FindFirstChild("Humanoid") == nil then Instance.new("Humanoid",monster) end
        exp = exp + 25
        print(exp)
        game.StarterGui.StatsGui.Exp.Text = "Exp: "..exp.."/"..expreq
        print(game.StarterGui.StatsGui.Exp.Text)
end

    if monster.Humanoid.Died:Wait() then 
monster.Humanoid.Died:Connect(monsterExp)
end

Answer this question