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:
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…
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.
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)
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
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)