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)
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)
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.
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
if BadgeService:UserHasBadgeAsync(player.UserId, 2124633610) then
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)
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.)