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

attempt to index upvalue 'Character' (a nil value)?

Asked by 5 years ago

I have try over and over and over again so many times but this is the same type of error I'm facing again.They said that 'Character' is a nil value.I don't understand what they mean.

I'm trying to make a projectile get thrown by using UserInputService.

local player = game.Players.LocalPlayer
local Character = player.Character
local db = true

UIS.InputBegan:Connect(function(Input, IsTyping)
    if IsTyping then return end
    local KP = Input.KeyCode
    if KP == Enum.KeyCode.E and db then
    if db == true then
    db = false
    mic:Clone()
    mic.Name = "MicToss"
    mic.Parent = Character
    mic.CFrame = Character.HumanoidRootPart.CFrame * CFrame.new(0.5,0,-5)

    local BV = Instance.new('BodyVelocity')
    BV.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
    BV.Velocity = Character.Torso.CFrame.LookVector * 100
    BV.Parent = mic
    end
end

It is line 14 where I get the error.

0
if Character ~= nil then DeceptiveCaster 3761 — 5y
0
works every time DeceptiveCaster 3761 — 5y
0
oh rochel89 42 — 5y
0
where are you running this script from? User#5423 17 — 5y
0
A local script in StarterPack rochel89 42 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

its probably because the script ran before the character ran, so if the character doesn't exist then it'll return nil. Use player.CharacterAdded:Wait() which yields until the character loaded or respawned.

local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait() 
local db = true

UIS.InputBegan:Connect(function(Input, IsTyping)
    if IsTyping then return end
    local KP = Input.KeyCode
    if KP == Enum.KeyCode.E and db then
    if db == true then
    db = false
    mic:Clone()
    mic.Name = "MicToss"
    mic.Parent = Character
    mic.CFrame = Character.HumanoidRootPart.CFrame * CFrame.new(0.5,0,-5)

    local BV = Instance.new('BodyVelocity')
    BV.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
    BV.Velocity = Character.Torso.CFrame.LookVector * 100
    BV.Parent = mic
    end
end
0
The player.CharacterAdded thingy worked but there is an error saying CFrame is not a valid member of Model.My thing is a model but is there anything i can do to fix this? rochel89 42 — 5y
0
CFrame isnt a property of Model, instead make a part as the root and set it as the PrimaryPart User#23365 30 — 5y
1
then use :SetPrimaryPartCFrame() on the model to set the CFrame of it User#23365 30 — 5y
0
If there is no primary part there, use the TranslateBy(Vector3 delta) function of model. Instead of offsetting from a single part, it'll offset from the center of the model. EzraNehemiah_TF2 3552 — 5y
0
Ok thanks,I'll try it later rochel89 42 — 5y
Ad

Answer this question