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

Why am I getting that my variable(Player) is a nil value?

I am trying to make a code that whenever a person presses the key e, a ball appears in front of the player. I have yet to add the velocity because I dont know how, and the CFrame from this code, I just got from youtube because i do not understand it. May anybody pls help me.

local Player = game.Players.LocalPlayer
game:GetService("UserInputService").InputBegan:connect(function(input)
    if input.KeyCode == Enum.KeyCode.E then
        local x = Instance.new("Part")
		x.Shape = "Ball"
		x.CanCollide = false
		x.TopSurface = "Smooth"
		x.BottomSurface = "Smooth"
		x.CFrame = Player.Character.UpperTorso.CFrame * CFrame.new(0,3,-12)
    end
end)

3 comments

0

wait(0.5) at the start

0

Could you expand on what you mean? How's the player nil? What do you mean by that? Is the script your using a LocalScript? You also didn't parent your part, and I recommend you, instead of getting the torso(s), retrieve the HumanoidRootPart for that purpose; much better, and shortens code. All in all, please be more descriptive.

0

Whenever I try to run my script by pressing e in the game, in the output it says, "Player is a nil value", Nope, I was using a regular script in the scriptservicescript. and what do u mean by parenting my part? Also, what do you mean by getting the torsos... I was trying to position the fireball to the torso by using CFrame which I dont understand, the cframe code that I used was from a video. Al

1 answer

✓ Accepted answer

This script needs to be a LocalScript in a location like StarterPlayer > StarterCharacterScripts

local Player = game.Players.LocalPlayer
local char = Player.Character or Player.CharacterAdded:Wait()
local head = char:WaitForChild("Head")

-- Just make it once, clone it later.
local x = Instance.new("Part")
x.Shape = "Ball"
x.CanCollide = true
x.TopSurface = "Smooth"
x.BottomSurface = "Smooth"
x.Size = Vector3.new(3,3,3);

game:GetService("UserInputService").InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		local cl = x:Clone()
		cl.Parent = Workspace -- You forgot to Parent it
		cl.Position = head.Position + (head.CFrame.lookVector * 2) 
		-- Position of head + push it in the direction the 
		-- player is looking and * 2 away from the player
	end
end)


2 comments

0

Hello! TYSM THIS WORKED, but what does line 17 do, can you break it down for me? I dont understand Cframe and Idk what lookvector is..

0

lookVector is based on the front face of a brick. It'd work for any brick, not just a players head. Research CFrame and lookVector.

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