Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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)

4 comments

0

Are you making it so when the monster dies, it rewards exp?

0

Yes

0

use humanoid.died if the monster has humanoid if not you can add one

0

is this a script or localscript? and where is the script located?

2 answers

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)

1 comment

0

still getting the same problem, the first time the function works, but the second time it doesnt

    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

This discussion is preserved as it was. New answers, comments, and votes are closed.