Different EXP rewards for different versions of an Enemy

I’ve already made a topic about this script but that was focused more on the SuperEnemy itself, I’m now trying to implement the higher rewards for defeating said SuperEnemy

My script works and gives SuperReward but only for 2 SuperEnemy kills then it just gives NormalReward EXP and Gold rewards

I’ve been fiddling with script:Remove() vs script:Destroy() but that really didn’t change anything

It’s weird because its only the SuperReward function that breaks, not the NormalReward function and I’m wondering if it has something to do with how it respawns. Any help is appreciated.

Here’s what I have:

local Humanoid = script.Parent.Enemy
local EXPdisplay = game.StarterGui.EXPAmountDisplay
local chance = math.round(100/75)

local function NormalReward()
	local tag = Humanoid:FindFirstChild("creator")
	if tag ~= nil then
		if tag.Value ~= nil then
			local Leaderstats = tag.Value:FindFirstChild("leaderstats")
			if Leaderstats ~= nil then
				Leaderstats.Exp.Value = Leaderstats.Exp.Value + 20
				Leaderstats.Gold.Value = Leaderstats.Gold.Value + 15
				wait(0.1)
				script:Destroy()
			end
		end
	end
end

local function SuperReward()
	local tag = Humanoid:FindFirstChild("creator")
	if tag ~= nil then
		if tag.Value ~= nil then
			local Leaderstats = tag.Value:FindFirstChild("leaderstats")
			if Leaderstats ~= nil then
				Leaderstats.Exp.Value = Leaderstats.Exp.Value + 75
				Leaderstats.Gold.Value = Leaderstats.Gold.Value + 50
				wait(0.1)
				script:Destroy()
			end
		end
	end
end


local function SuperEnemy()
	if not Humanoid then return end

	if math.random(chance) == 1 and Humanoid.MaxHealth == 15  then
		Humanoid.MaxHealth *= 2.5
		Humanoid.Health = Humanoid.MaxHealth

		local sparkles = Instance.new("Sparkles")
		sparkles.Parent = script.Parent:FindFirstChild("Torso")
		
		return true
	end
	
	return false
end

local isSuperEnemy = SuperEnemy()

if isSuperEnemy then 
	Humanoid.Died:Connect(SuperReward)
else
	Humanoid.Died:Connect(NormalReward)
end
1 Like

how is enemy spawning determined in your game?? from the look of the script, you wouldn’t guess there is anything wrong with it…

the only issues i have in mind is:

  1. the enemy is being cloned while already a super enemy
  2. the script is destroyed for the new enemy as well

also, i am 99.9% sure math.random only supports integers, aka whole numbers, so your super enemy chance is currently guaranteed

1 Like

You have two functions for Reward, you could replace them with one function instead, and that could help you debug the problem better

local function Reward(exp, gold)
	local tag = Humanoid:FindFirstChild("creator")
	if tag ~= nil then
		if tag.Value ~= nil then
			local Leaderstats = tag.Value:FindFirstChild("leaderstats")
			if Leaderstats ~= nil then
				Leaderstats.Exp.Value = Leaderstats.Exp.Value + exp
				Leaderstats.Gold.Value = Leaderstats.Gold.Value + gold
				wait(0.1)
				script:Destroy()
			end
		end
	end
end
if isSuperEnemy then 
	Humanoid.Died:Connect(function() Reward(75, 50) end) --lambda functions so you can provide arguments
else
	Humanoid.Died:Connect(function() Reward(20, 15) end)
end

I’d personally recommend not having this script under each Enemy, and instead have one script that runs a function for each Enemy when they are created. There could be a chance that this script isn’t being initialized under the SuperEnemy somehow

1 Like

The chance is mainly for testing im not worried about that, but the respawning is handled in another script within the enemy

There also is another problem where once the enemy has become a SuperEnemy and dies, it’ll keep becoming a SuperEnemy and never become a normal enemy again.

I also had the thought that I should combine the respawning script with the EXP script but im unsure on how that would go.

Here it is incase that helps, I will try to debug your inital suggestions though.

local Humanoid = script.Parent.Enemy
local Clone = script.Parent:Clone()

Humanoid.Died:Connect(function()
	wait(5.5)
	script.parent:Destroy()
	Clone.Parent = game.Workspace
end)
  1. Your superenemy chance is currently 100%. math.random(1) is ALWAYS 1.

  2. A possibility as to why your code could be breaking is that this statement:

if math.random(chance) == 1 and Humanoid.MaxHealth == 15  then
		Humanoid.MaxHealth *= 2.5

…will cause your code not to run if you recycle the same NPC objects, since MaxHealth will be >15.

  1. Consider using CollectionService to add functionality to NPCs rather than relying on per-enemy scripts.