Chance Script not working correctly

Hello, I have three identical dummies with all the same parts and each has this seperate script which is supposed to make a random number called chance and if the chance is equal to a certain number then drop the item but when printing it only creates the chance variable once and prints it once. Thanks for any help.

local dummy = script.Parent

dummy.Humanoid.HealthChanged:Connect(function()
	if dummy.Humanoid.Health <= 0 then
		local chance = math.random(1,10)
		print(chance) -- Prints chance variable only once even when another dummy is destroyed
		if chance == 5  then-- 100% chance 
			local gd = game.ReplicatedStorage.GoldenBall:Clone()
			gd.Parent = workspace
			gd.Position = dummy.HumanoidRootPart.Position
			dummy:Destroy()
		elseif chance <= 5 then
			local ammo = game.ReplicatedStorage.Dodgeball:Clone()
			ammo.Parent = workspace
			ammo.Position = dummy.HumanoidRootPart.Position
			dummy:Destroy()
		else
			dummy:Destroy()
		end
		end
	end)


robloxapp-20240525-1727582.wmv (2.5 MB)

The output in this example printed only 7 once

Instead of using HealthChanged and checking the health replace all of that with a Humanoid.Died event. HealthChanged should also work though, and it passes the new health as a variable (like the BasePart.Touched event passes the touched part). Maybe if you checked that instead of Humanoid.Health, then you would have more prints.

1 Like

it only checks if the dummy has less than 0 health lol

so change it to > 0

No, this is supposed to happen on death. That’s why it’s <=.

1 Like