Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by
KordGamer 155
9 years ago

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

Log in to vote
1
Answered by 9 years ago

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

0
Hi UserOnly16Characters, Thanks for helping out! It works and everything! KordGamer 155 — 9y
0
No problem,I would appreciate it if you could accept my answer! :) UserOnly20Characters 890 — 9y
0
Sorry, But how do I accept an answer? I am really new to this website. xD KordGamer 155 — 9y
0
Beneath the "ANSWERED BY" thing there is this thing called "Accept Answer" which will give you and me one reputations. UserOnly20Characters 890 — 9y
View all comments (2 more)
0
Ctrl + F , search Accept Answer ImageLabel 1541 — 9y
0
It's not there O_O KordGamer 155 — 9y
Ad

Answer this question