I'm not sure if this would work or not

I want to make it so a player can only get a badge when they touch and part and they are still alive.
I’m not sure if the script I made would work or not.
I don’t think I would be able to get the answer just by looking on here but maybe I would be able to. I wasn’t sure if Scripting Support or Code Review was better for this. I just want to know if I made the script correctly so that it will only award the badge if the player touches the part and is still alive.
Here is 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 = __PART.Parent:FindFirstChild("Humanoid")
	if __PLAYER ~= nil and (not humanoid.Health) == 0 then
		print("Here is your badge")
		__BADGESERVICE:AwardBadge(__PLAYER.UserId, __ID)
	end
end)
1 Like

first you want to make sure humanoid exists before checking its health
other than it, if the script is a server script it will work fine

So would I need to make sure the script works by doing this?:

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 __PLAYERS.character.humanoid then
	if __PLAYER ~= nil and (not humanoid.Health) == 0 then
		print("Here is your badge")
		__BADGESERVICE:AwardBadge(__PLAYER.UserId, __ID)
	end
end)

Or would it work without that?

this to this

if not humanoid then return end

1 Like

Like this?:

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)
3 Likes

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