UserId is not a valid member of Players "Players"

Hello! I tried making a puzzle game where you find a keycard and if you do, it awards you a badge. I tried making it and it didn’t work.
Here is output: Screenshot_168

code:

script.Parent.Touched:Connect(function(card)
	if card.Parent.Name == "Destroyer 2" then
		spam_preventer = true
		-- Rewarder --
		local Player = game:GetService("Players")
		game.BadgeService:AwardBadge(Player.UserId, 2124805361) -- error of problem
		local h = Instance.new("Message",workspace)
		h.Text = "Congratulations! You have beaten the game. Due to this being in alpha, nothing will be awarded, soon it will be a badge!"
		wait(9)
		h.Text = "You may now leave."
		wait(5)
		for i,v in pairs(game.Players:GetPlayers()) do
			v:Kick("You have beaten the game. Congratulations!")
			wait(99 * 99)
			spam_preventer = false
		end
	end
end)

I tried looking at Developer hub, and dev forums, no answer.

sorry if this looks idiotic, I’m a bit new to scripting…

1 Like

As it says you have not tried to reference a player’s user id but rather the user id of the service “Players” which has no property called user id.
You need to check if it is a player and if it is then take the player’s user id.

You’re referencing the Players service. Not the actual player itself.

Check if the hit has a humanoid, and if it does, tell the Players service to GetPlayerFromCharacter(), then award the badge from there.

Hope it works for you.

1 Like
	if card.Parent.Name == "Destroyer 2" then
		spam_preventer = true
		-- Rewarder --
		local Player = game.Players.LocalPlayer -- Try this or GetPlayerFromCharacter() if it's a regular script.
		game.BadgeService:AwardBadge(Player.UserId, 2124805361) -- error of problem
		local h = Instance.new("Message",workspace)
		h.Text = "Congratulations! You have beaten the game. Due to this being in alpha, nothing will be awarded, soon it will be a badge!"
		wait(9)
		h.Text = "You may now leave."
		wait(5)
		for i,v in pairs(game.Players:GetPlayers()) do
			v:Kick("You have beaten the game. Congratulations!")
			wait(99 * 99)
			spam_preventer = false
		end
	end
end)

Try this, should help.

He referenced script.Parent.Touched though, indicating it’s a server script inside of a part in workspace. Therefore game.Players.LocalPlayer wouldn’t work. I could be wrong though.

Yeah, what I’m trying to do in the script is, if the keycard (a tool) the player held and puts it in a door, it will open and reward a badge like the kingdom room

Should be hit instead.

script.Parent.Touched:Connect(function(hit)
    if hit.Parent.Name == "Humanoid" then
       -- Insert Code.
    end
end)

Oh, then just refer to the one that labels game.Players.LocalPlayer then

Two issues with your script

  • Your referencing Local Player which I assume is on a Script and not Local
  • If the script is a Local Script you will get another error cause you can’t award badges to players on the client, only the server.

Right, should be then game.Players:GetPlayerFromCharacter()

Screenshot_169

You added a full stop instead of a comma. And did not provide a parameter to GetPlayerFromCharacter.

It’ll be

local Player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
game:GetService("BadgeService"):AwardBadge(Player.UserId, 2124805361)

he’s using a tool, it has to be a tool object instead.

I’ve went through and remade your script a bit, I’m honestly not sure how your game functions so Instead of blocking every player from being able to get it once one person touches It I just set up a Player Cooldown instead.

local ServerCooldownEvents = {}
local PlayerService = game:GetService("Players")
local BadgeService = game:GetService("BadgeService")

local function ConnectCooldown(UserId, Indicator, Preasure)
	if not ServerCooldownEvents[Indicator] then
		ServerCooldownEvents[Indicator] = {}
	end 
	if ServerCooldownEvents[Indicator][UserId] then
		return true
	else
		ServerCooldownEvents[Indicator][UserId] = true
		delay(Preasure, function()
			ServerCooldownEvents[Indicator][UserId] = false
		end)
		return false
	end
end

script.Parent.Touched:Connect(function(hit)
	if hit.Parent.Name == "Destroyer 2" then
		local Tool = hit.Parent
		local Player = PlayerService:GetPlayerFromCharacter(Tool.Parent)
		
		warn(hit.Name, hit.Parent)
		if Player ~= nil then
			warn(Player.Name.." Touched the part wit a tool")
			
			if ConnectCooldown(Player.UserId, "Card_Delay", 30) then
				return
			end
			
			BadgeService:AwardBadge(Player.UserId, 2124805361)
			
			local Message = Instance.new("Message", workspace)
			Message.Text = "Congratulations! You have beaten the game. Due to this being in alpha, nothing will be awarded, soon it will be a badge!"
			wait(9)
			Message.Text = "You may now leave."
			
			Player:Kick("You have beaten the game. Congratulations!")
		end
	end
end)