I'm not sure how to make it so when a player touches a part they get a badge but ONLY if they are alive

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make it so that when a player touches a part they get a badge but ONLY if they are alive.
  2. What is the issue? Include screenshots / videos if possible!
    It’s a bit difficult for me to test it so I’m trying to get feedback on a script I already have to make sure it would work.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I don’t think I would be able to test it here by looking at other topics. I might be able to find a script that works but I don’t want to make a entire new script when I have one that already works considering I had many issues awarding a player a badge when they touch a part in the first place.
    Here is the script that I have:
local __BADGESERVICE = game:GetService("BadgeService")
local __PLAYERS = game:GetService("Players")
local __ID = 2140926703 -- !! REPLACE

script.Parent.Touched:Connect(function(__PART)
	local __PLAYER = __PLAYERS:GetPlayerFromCharacter(__PART.Parent)
	local humanoid = __PART.Parent:FindFirstChild("Humanoid")
	if not humanoid then
		return end
		if __PLAYER ~= nil and (not humanoid.Health) == 0 then
			print("Here is your badge")
		__BADGESERVICE:AwardBadge(__PLAYER.UserId, __ID)
	end
end)

I haven’t gotten a confirmation that this works yet or a correction on something. I can’t tell if I should use if or else if on line 10 of the script.

local __BADGESERVICE = game:GetService("BadgeService")
local __PLAYERS = game:GetService("Players")
local __ID = 2140926703 -- !! REPLACE

script.Parent.Touched:Connect(function(__PART)
	local __PLAYER = __PLAYERS:GetPlayerFromCharacter(__PART.Parent)
	local humanoid = __PLAYER.Character:FindFirstChild("Humanoid")
	if not humanoid then
		return end
		if __PLAYER ~= nil and humanoid.Health > 0 then
			print("Here is your badge")
		__BADGESERVICE:AwardBadge(__PLAYER.UserId, __ID)
	end
end)

There.

3 Likes

Thank you for helping me out again. I released at some point when developing the game that the player might be able to die but still get the badge which would be a problem.

1 Like

The reason this wasn’t working before is because of your logic.

Lua read what’s in the parenthesis first, not humanoid.Health, which is false.
Then lua checked false == 0, which is always false. All of these conditions would also work:

not humanoid.Health == 0
humanoid.Health ~= 0
humanoid.Health > 0

3 Likes

I didn’t know if it would work in the first place. I never tested it because it would be more difficult to do. I figured it would be best to check if anything was wrong here. The reason I put parenthesis is because in the script it told me to do so.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.