Sparkles not showing on players head

Hey, so I’ve been wanting to make my characters head sparkle and the script does not work, keeps giving me this error:

01:05:32.880  ServerScriptService.SparklingScript:5:
 attempt to index nil with 'Head'  -  Server - SparklingScript:5

Here is the script:

local sparkles = Instance.new("Sparkles")

local character = game.Workspace:FindFirstChild("VakarisLDoE")

sparkles.Parent = character.Head

Can someone help me figure this out? Not sure how to fix this, looks like everything is fine but it isn’t.

2 Likes
local character = game.Workspace:WaitForChild("VakarisLDoE")
sparkles.Parent = character:WaitForChild("Head")

WaitForChild basically does what it says, wait for the child of an instance until is fully loaded

How is the character variable defined?

So my script wasn’t working because the instances weren’t loaded yet? I have to wait for them to load first before applying anything?

exactly, this happens when you use a local script and you want to detect things without waiting the game to load properly. also, you should use “WaitForChild()” only one time for each instance. with server scripts this doesnt apply. unless the instance you are searching is in replicated storage

In order to find out the current player in the local script, you need to use the localPlayer method, and character already follows from it.

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

Also, I didn’t just take the character, but also waited for it in case the character was being revived at that time.

or player.CharacterAdded:Wait()

yeah this is correct, but what if the character hes searching is a rig and not a player? thats why i wont modify scripts unless the owner explains what he wants to achieve

The thing is that this script is not a local one and it is in ServerScriptService. I want to make it so all players in my game have sparkles and you can’t do that with a local script, at least I don’t think so…

you should still use this method

btw, you asked for all the players, so here’s another script:

game.Players.PlayerAdded:Connect(function(player)
   player.CharacterAdded:Connect(function(character)
     sparkles:Clone().Parent =  character:WaitForChild("Head")
   end
end

this script detect every player who joins the game and everytime their character is added, if so, it clones the sparkles particle and modify his parent to the player’s head

I should use this alongside the other script right?

Oh and also, you forgot two parenthesys near the two ends:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		sparkles:Clone().Parent =  character:WaitForChild("Head")
	end)
end)

nope, the other script works only for one “player”, this one for everyone and everytime

So I should only define sparkles and that’s it? Everyone who joins should have sparkles right?

yes and yes

30charfillerrrrrrr

@deaconstjohn03 and @Super_God567 thanks for the help!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.