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 upvalue 'Character' (a nil value)?

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.

5 comments

0

if Character ~= nil then

0

works every time

0

oh

0

where are you running this script from?

0

A local script in StarterPack

1 answer

✓ Accepted answer

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

5 comments

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?

0

CFrame isnt a property of Model, instead make a part as the root and set it as the PrimaryPart

1

then use :SetPrimaryPartCFrame() on the model to set the CFrame of it

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.

0

Ok thanks,I'll try it later

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