Badge Award System wont work with emitter

Hey all! Simple issue with probably a simple answer… Basically,

I have a particle emitter and a script.
The script will pick a random number (temp 0-10) and if its == 0 then it will enable the emitter. Here is the surprise, when the emitter enabled i was going to have everyone on the server receive a badge. (If fire flies are enabled people will get a badge for seeing them.

However, the script keeps on telling me Line 12 is nil.

(badgeservice:AwardBadge(char.UserID,badgeID)

Most of the numbers are temp but any help would be great.

here is the full code.

local player = game.Players.LocalPlayer
local badgeID = 2126443484
local badgeservice = game:GetService("BadgeService")


while wait(1)do
	SpecialNumber = math.random(0,10)
	if SpecialNumber == 0 then
		script.Parent.ParticleEmitter.Enabled = true
		local plr = game:GetService("Players")
		local char = plr.LocalPlayer
		badgeservice:AwardBadge(char.UserId,badgeID) -- This is line 12
		print("Badge Awarded")
	end
	if SpecialNumber >= 1 then
		script.Parent.ParticleEmitter.Enabled = false
	end
	print(SpecialNumber)
end

char variable are useless when your player variable have LocalPlayer, you can use player variable as replacement of line 12

edit: here the modified script

local player = game.Players.LocalPlayer
local badgeID = 2126443484
local badgeservice = game:GetService("BadgeService")


while wait(1)do
	SpecialNumber = math.random(0,10)
	if SpecialNumber == 0 then
		script.Parent.ParticleEmitter.Enabled = true
		-- delete another player variable, because at line 1, is already have (bug 1)
		badgeservice:AwardBadge(player.UserId,badgeID) -- This is line 12, replace with the player variable in line 1
		print("Badge Awarded")
	end
	if SpecialNumber >= 1 then
		script.Parent.ParticleEmitter.Enabled = false
	end
	print(SpecialNumber)
end

I totally didn’t see this im so sorry, lemme try what you sent me, thank you so much! :smiley:

you can reply as feedback about the modified script! no problem

Ight so i did what i needed and it tells me. line 11 (actually line 12, the player.userId, badgeID line) and it tells me

attempt to index nil with ‘UserID’.

sorry to pester, thank you!

Slight grammar mistake, its UserId, not UserID

It is UserId? did you mean badgeId? :slight_smile:

Is this in a local script or a server script?

server script. Sorry thats my fault for not clarifying!

Oh then you can’t use Local Player in a server script, do you just want the badge to only be given to one person or the entire server?

you cant use LocalPlayer in ServerScripts

local badgeID = 2126443484
local badgeservice = game:GetService("BadgeService")

game.Players.PlayerAdded:Connect(function(player)
while wait(1)do
SpecialNumber = math.random(0,10)
if SpecialNumber == 0 then
script.Parent.ParticleEmitter.Enabled = true
-- delete another player variable, because at line 1, is already have (bug 1)
badgeservice:AwardBadge(player.UserId,badgeID) -- This is line 12, replace with the player variable in line 1
print("Badge Awarded")
elseif SpecialNumber >= 1 then
script.Parent.ParticleEmitter.Enabled = false
end
print(SpecialNumber)
end
end)

You can’t award Badge in Client. I recommend using RemoteEvent.

on particle emitter, it would server script because

its 3d object

(Correction Text Of My Mistake Above, Within Chars System Dumb)

sorry @msix29 , But PlayerAdded Now Change Into Function

local badgeID = 2126443484
local badgeservice = game:GetService("BadgeService")

local function onPlayerAdded(player)
 -- The Script
end

game.Players.PlayerAdded:Connect(onPlayerAdded)

if i wrong, sorry @msix29

there is no difference between our scripts, you made it as a separate function but i kept it as a single function, they will work in the same way though.

Yeah, imma have to figure it out that way then? Because sadly despite everyone’s help- it doesn’t work Dx. Do you know any good tutorials on how to fully understand and utilize remote events?

HERE is a tutorial about remote events by AlvinBlox

1 Like

Step1: Put RemoteEvent in ReplicatedStorage and name it “AwardBadgeRemote”.
Step2: Put Script [NOT LOCAL] in ServerScriptService.
And code like:

local BadgeService = game:GetService("BadgeService")
local badgeId = 00000000 -- Replace to BadgeId

game.ReplicatedStorage.AwardBadgeRemote.OnServerEvent:Connect(function(player)
	BadgeService:AwardBadge(player.UserId, badgeId)
end)

Step3: Put LocalScript in StarterGui.
Code:

local Player = game.Players.LocalPlayer
local ParticleEmitter = -- Locate to ParticleEmitte.

while task.wait(1) do
	local SpecialNumber  = math.random(1,10)
	
	if SpecialNumber == 1 then
		ParticleEmitter.Enabled = true
		game.ReplicatedStorage.AwardBadgeRemote:FireServer()
		print("Badge Awarded")
	else
		if SpecialNumber >= 2 then
			ParticleEmitter.Enabled = false
		end
		print(SpecialNumber)
	end
end

If this work, please mark this post as Solution. Thank you!

1 Like

I see what you mean and it works! Thank you so much!!! and thank you to everyone else that helped, I really appreciate it. There are still certain aspects of LUA code i don’t understand xD.