I'm writing a script to make coins fall out of a player on death. It's a localscript and it is in StarterGUI. I get the error 'attempt to index local 'Character' (a nil value)'
How could I fix this?
This is the code :
local Player = script.Parent.Parent local Character = game.Workspace:FindFirstChild(Player.Name) local CharTorso = Character.Humanoid function onDied() for i =1,script.GoldCount.Value do local xp = Instance.new("Part") xp.Parent = game.Workspace xp.Position = Character.Torso.Position + Vector3.new(math.random(-3,3),math.random(-3,3),math.random(-3,3)) xp.Shape = 0 xp.formFactor = "Symmetric" xp.Name = "XP" xp.BrickColor = BrickColor.new(24) xp.Reflectance = 0.5 xp.Size = Vector3.new(1,1,1) local clone = script.Increase:Clone() clone.Disabled = false clone.Parent = xp end end CharTorso.Died:connect(onDied)
Any help is appreciated!
The problem is your trying to access the Player's Character using FindFirstChild()
. Which would work if the character that died was called Player.Name
but it its not called Player.Name
the script won't take any further action (Not work). But however there a easier way to to access the players Character by using Parameters. I'll post a example of how to use a parameter.
game:GetService('Players').PlayerAdded:connect(function(player) player.CharacterAdded:connect(function(character) character:WaitForChild("Humanoid").Died:connect(function(Died)-- I called my Parameter "Died" but you call your parameter different. print(character.Name)--This print and the name of the player who died. end) end) end)
This script activate every time someone Dies which means you can use it to make your "Xp script". If you want more about the events and function I used. Parameters, Died Function