Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

attempt to index local 'Character' (a nil value) - How can I fix this? - [Help needed ASAP]

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!

1 answer

✓ Accepted answer

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.Namethe 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

6 comments

0

Hi UserOnly16Characters, Thanks for helping out! It works and everything!

0

No problem,I would appreciate it if you could accept my answer! :)

0

Sorry, But how do I accept an answer? I am really new to this website. xD

0

Beneath the "ANSWERED BY" thing there is this thing called "Accept Answer" which will give you and me one reputations.

0

Ctrl + F , search Accept Answer

0

It's not there O_O

This discussion is preserved as it was. New answers, comments, and votes are closed.