Why this script doesn't work ? (badge arward)

Hi, does anyone have any idea how to make this script work? The problem comes only from line one because if I change it to game.Workspace.PartY.Touched:Connect(function(ok) then as soon as the player touches the part the badge is obtained, except that it’s not what I want (but you can understand that the script works fine), I put only the problematic part of the script. Thank you

game.Players.PlayerAdded:Connect(function(ok) -- If i put game.Workspace.PartY.Touched:Connect(function(hit) it work ! So the problem is in this line
 if ok.Parent:FindFirstChild("Humanoid") then
			local character = ok.Parent
			local player = game.Players:GetPlayerFromCharacter(character)
			if (player:WaitForChild("Creatures"):FindFirstChild("testbadge")) then
			if player then
				giveBadge(player, 2126218668)
	       end
         end
		end
end)

The Player instance (ok) is not the physical character, you need to wait for ok.Character to exist in workspace before referencing the humanoid from it.

1 Like

The parameter ok is a player, or the player that just joined.

ok.Parent is the players service (or game.Players)
To get the character you can do
local char = player.CharacterAdded:Wait()

Anyways most of your code is unnecessary, it can be reduced to simply:


game.Players.PlayerAdded:Connect(function(player)
    local character = player.CharacterAdded:Wait()
    if (player:WaitForChild("Creatures"):FindFirstChild("testbadge")) then
        giveBadge(player, 2126218668)
    end
end)

2 Likes

Use this:

--//Services
local Players = game:GetService("Players")

--//Functions
Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local creatures = player:WaitForChild("Creatures", 5)
		
		if creatures:FindFirstChild("testbadge") then
			giveBadge(player, 2126218668)
		end
	end)
end)
1 Like

Thank you very much for your answers !
Both scripts work

1 Like