What do you want to achieve?
I want to give players a badge when they touch a part if they have a certain value of their IntValue in their player instance.
What is the issue?
I get this error:
Did you look for solutions on the Developer Hub?
Yes.
local db = {}
local badgeID = 2129940979
local bs = game:GetService("BadgeService")
script.Parent["Meshes/Token_Token_Base"].Touched:Connect(function(hit)
local char = hit.Parent
local plr = game:GetService("Players"):GetPlayerFromCharacter(char)
if plr:FindFirstChild("Stars").Value >= 5 then -- error on this line
table.insert(db,char.Name)
end
if table.find(db, char.Name) ~= nil then
table.insert(db,char.Name)
plr.Stars.Value += 1
if plr.Stars.Value == 5 then
bs:AwardBadge(plr.UserId, badgeID)
end
end
end)
local db = {}
local badgeID = 2129940979
local bs = game:GetService("BadgeService")
script.Parent["Meshes/Token_Token_Base"].Touched:Connect(function(hit)
local char = hit.Parent
local plr = game:GetService("Players"):GetPlayerFromCharacter(char)
if not plr then return end --this is what I changed (added)
if plr:FindFirstChild("Stars").Value >= 5 then -- error on this line
table.insert(db,char.Name)
end
if table.find(db, char.Name) ~= nil then
table.insert(db,char.Name)
plr.Stars.Value += 1
if plr.Stars.Value == 5 then
bs:AwardBadge(plr.UserId, badgeID)
end
end
end)
Have you forget to put the Folder’s name right before the Stars Value?
Most of the people use the Leaderstats To Their Folder Name On Their Data.
That’s how your script should be:
local db = {}
local badgeID = 2129940979
local bs = game:GetService("BadgeService")
script.Parent["Meshes/Token_Token_Base"].Touched:Connect(function(hit)
local char = hit.Parent
local plr = game:GetService("Players"):GetPlayerFromCharacter(char)
if plr:FindFirstChild("leaderstats").Stars.Value >= 5 then -- error on this line
table.insert(db,char.Name)
end
if table.find(db, char.Name) ~= nil then
table.insert(db,char.Name)
plr.leaderstats.Stars.Value += 1
if plr.leaderstats.Stars.Value == 5 then
bs:AwardBadge(plr.UserId, badgeID)
end
end
end)
I’ll be sure to add that in, but either way this error only comes when my character touches it. In this case, plr is a player and yet its saying that it is nil.
Did you see if it pass or not? try this and tell me what prints out.
local db = {}
local badgeID = 2129940979
local bs = game:GetService("BadgeService")
script.Parent["Meshes/Token_Token_Base"].Touched:Connect(function(hit)
local char = hit.Parent
local plr = game:GetService("Players"):GetPlayerFromCharacter(char)
if not plr then return end --this is what I changed (added)
print("There is a player")
if plr:FindFirstChild("Stars").Value >= 5 then -- error on this line
table.insert(db,char.Name)
print("Found stars.")
end
if table.find(db, char.Name) ~= nil then
print("Found in table")
table.insert(db,char.Name)
plr.Stars.Value += 1
if plr.Stars.Value == 5 then
print("Stars is equal to 5")
bs:AwardBadge(plr.UserId, badgeID)
end
end
end)