Activating local function is failing

This script works properly when I use the function within radius of a player, but I am not sure why the kill and badge function doesn’t work.

local function AwardBadge(player, str)
	if not player and badgeservice:UserHasBadgeAsync(player.UserId, str) then
		badgeservice:AwardBadge(player.UserId, str)
	end	
end	

local function Kill(player, plr2)
	player.leaderstats.Value += 5
	plr2.Character.humanoid.Health = 0
	plr2.leaderstats.Coins.Value -= 2
end

findradius.OnServerEvent:Connect(function(player, kniferadius)
	local currentPlayers = Players:GetPlayers()

	for _, ply in pairs(currentPlayers) do
		-- Gets Player table--
		if (player.Character.HumanoidRootPart.Position - ply.Character.HumanoidRootPart.Position).Magnitude < kniferadius and ply.UserId ~= player.UserId then
			-- If any of the players on the player table are within radius of player who fired server then--
			findradius:FireClient(player, 'playknifesfx')
			Kill(player, ply)
			AwardBadge(player, badges.FirstKill)
			-- Fires clients and functions--
		end
	end
end)

As far as the Kill() function not working, it’s because you aren’t correctly defining the character’s humanoid, which is case-sensitive. Also, most typically use :WaitForChild("Humanoid") when trying to grab a Humanoid since it, in my experience, usually doesn’t work without using :WaitForChild().

The script below should work for Kill(), let me know if anything errors and let me know if you need anything else! :wink:

Revised Kill() Function
local function Kill(player, plr2)
        local eChar = plr2.Character or plr2.CharacterAdded:Wait() -- Making sure to get the enemy character.
        local eHum = eChar:WaitForChild("Humanoid") -- Grabbing enemy Humanoid.
        eHum:TakeDamage(eHum.MaxHealth) -- Killing them.
        player.leaderstats.Value += 5
        eChar.leaderstats.Coins.Value -= 2
    end
1 Like

Sorry for the delayed response, but I believe I’ve also found the solution for your AwardBadge() function. I think it isn’t working because of the wording, since you already have everything you need in the function. Let me know if the snippet below works and if it errors leave a reply!

Hope this helps, code snippet below.

Revised AwardBadge() Function
local function AwardBadge(player, str)
	if not player or badgeservice:UserHasBadgeAsync(player.UserId, str) then -- Using the "or" operator because by using "and" you're stating, "If the player == nil/false and they have the badge, then don't give them the badge." However, by using "or" you're saying "If the player is nil/false, then don't give them the badge. Furthermore, if the player isn't nil/false, then check if they have the badge and if they still do don't give them the badge!"
		return
    else
        badgeservice:AwardBadge(player.UserId, str) -- If the player satisfies both prequisites, i.e being a player and not having the badge, then give them the badge!
	end	
end	
1 Like

Omw your a lifesaver bro I was trying to figure that out

1 Like

Glad I could help my friend. :wink:

Let me know if you need anything else!

1 Like

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