Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Item not giving when user owns badge?

Asked by
yoshi8080 445 Moderation Voter
9 years ago

I wanted the player to get an item when owning a badge, but it doesn't seem to work?

local badge = game:GetService("BadgeService")
local badgeId = 18987678 
local player = game.Players.LocalPlayer
local tool = game.ServerStorage:WaitForChild('BloxyCola')

if badge:UserHasBadge(player.userId, badgeId) then
local newtool = tool:Clone()         
newtool.Parent = player.Backpack
else
print("Player doesn't have")
end

This is the error Workspace.Script:6: attempt to index local 'player' (a nil value)

3 answers

Log in to vote
1
Answered by 9 years ago
game.Players.PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(character)
        if badge:UserHasBadge(player.UserId, badgeId) then
            tool:Clone().Parent = player.Backpack
        end
    end)
end)
0
may help to give a slight reasoning behind this answer LuaQuest 450 — 9y
0
Help who? I already explained the reasoning to yoshi in chat, and CodingEvolution explained the rest below (for anyone who comes to this particular post for a reason) chess123mate 5873 — 9y
Ad
Log in to vote
2
Answered by 9 years ago

The other answers are wrong. It doesn't matter whether you use "UserId" or "userId". The problem is in the error message "player (a nil value)"

The only time that error would happen is if you're trying to use "LocalPlayer" in a server script. That will only work in a local script.

However, you can only use the UserHasBadge function in a server script. So, let's change things up a bit:

-- Server script
-- Badge handler

-- Services
local Players = game:GetService'Players'
local BadgeServ = game:GetService'BadgeService'
local Storage = game:GetService'ServerStorage'

-- Configurable
local Tool = Storage:WaitForChild'BloxyCola'
local BadgeId = 18987678

-- Main
Player.PlayerAdded:connect(function(NewPlayer)
    if BadgeServ:UserHasBadge(NewPlayer.UserId, BadgeId) then
        Tool:Clone().Parent = NewPlayer.StarterGear
    end
end)

Let me know if you have any questions.

Log in to vote
1
Answered by 9 years ago

Problem Roblox changed player.userId http://wiki.roblox.com/index.php?title=API:Class/Player

Solution

if badge:UserHasBadge(player.UserId, badgeId) then

Answer this question