What's wrong with my Script?

Hello once again!

I know that something is wrong with my script but I don’t know what! I’m trying to give the player sparkles if they own a certain badge. Here is the script:

local BadgeService = game:GetService("BadgeService")
local sparkles = script.Parent.Parent.Workspace.EffB.Sparkles

game.Players.PlayerAdded:Connect(function(player)
	if BadgeService:UserHasBadgeAsync(player.UserId, 2124633610) then
		sparkles.Parent = player.Torso	
	end
end)

Can someone please point out what is wrong :smile:

Many thanks!

1 Like

I dont know where the Player’s Torso is stored.

game.Players.LocalPlayer.Torso ??

Is this a localscript or a server script?

1 Like

Oh sorry I forgot to mention.

It’s a normal Script (Server Script?) inside Server Script Service

You should clone the sparkles and put the clone inside their HumanoidRootPart. R15 rigs don’t have a torso.

There are a few things you’ll want to change here.

PlayerAdded runs when a player joins the game, not when their character loads in. You’ll want to do this:

game.Players.PlayerAdded:Connect(function(Player)
   --Player has joined the game
   Player.CharacterAdded:Connect(function(Character)
      --Player's Character has spawned
   end)
end)

What you have to check the badge is fine, so let’s add that in.

game.Players.PlayerAdded:Connect(function(Player)
   --Player has joined the game
   Player.CharacterAdded:Connect(function(Character)
      --Player's Character has spawned
      if BadgeService:UserHasBadgeAsync(Player.UserId, 2124633610) then
         	
      end
   end)
end)

Next, instead of doing sparkles.Parent = player.Torso, you need to do sparkles:Clone().Parent = player.Character.PrimaryPart. The :Clone() makes a copy of the sparkles, so we can give it to the player. If you don’t put :Clone(), you’ll give away the only copy of the sparkle effect and you’ll get an error if a second person joins or the first player dies and respawns. Players are the people themselves, Characters are the avatars running around ingame. R6 and R15 characters have differently named parts. Replacing “Torso” with “PrimaryPart” ensures that all players can get the effect.

game.Players.PlayerAdded:Connect(function(Player)
   --Player has joined the game
   Player.CharacterAdded:Connect(function(Character)
      --Player's Character has spawned
      if BadgeService:UserHasBadgeAsync(Player.UserId, 2124633610) then
         sparkles:Clone().Parent = Character.PrimaryPart
      end
   end)
end)
1 Like

I have my characters as R6. Can I still use PrimaryPart?

@R_alatch

Do you force all players to be R6? If so, you could still use Character.Torso.
If not, R15 players might join and it would be best to stick with Character.PrimaryPart.

PrimaryPart is a property of ALL MODELS (assuming the creator has set a part to be the PrimaryPart). All characters have a forced PrimaryPart, being the HumanoidRootPart. That means both R6 and R15 have a PrimaryPart, which is the HumanoidRootPart.

1 Like

Use Character.HumanoidRootPart or Character.PrimaryPart like he is saying, R6 rigs still have a HumanoidRootPart.

1 Like

I force the characters to be R6.

Im getting a red line under player:

game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
		if BadgeService:UserHasBadgeAsync(player.UserId, 2124633610) then

What does that mean?

its Player not player, thats how you defined it.

Try chaning

to

sparkles.Parent = workspace:FindFirstChild("Torso") -- UpperTorso if your game is R15.
1 Like

My bad, I always capitalize my variables. When I copy/pasted your badge conditional, I neglected to change your variable name.

1 Like

This is because you forgot the PlayerAdded event or because you named your variables incorrectly.

local BadgeService = game:GetService("BadgeService")
local sparkles = script.Parent.Parent.Workspace.EffB.Sparkles

game:GetService("Players").PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		if BadgeService:UserHasBadgeAsync(player.UserId, 2124633610) then
			sparkles:Clone().Parent = character.PrimaryPart
		end
	end)	
end)
1 Like

So should it be

(Player.UserId, 2124633610)
With a capital P at the start?

Just replace “player.UserId” with “Player.UserId”

1 Like

Yes because you defined it that way, so the capitalisation and spelling must be the same

1 Like

Oh sorry, that was a stupid mistake xD

It doesn’t really matter, you can name your variables any way you want to. It isn’t case sensitive. (Of course it is if you named it with a capital or lowercase letter and used the wrong type of letter later on.)

1 Like

It’s working now!

Thanks

:smile:

1 Like