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

How do i fix "attempt to index upvalue 'char' (a nil value)"?

Here's the script (btw its in a tool as a local script) what it's meant to do is when you press q and you have a certain amount of a stat then it will trigger a punch animation

math.randomseed(tick())

local player = game.Players.LocalPlayer
local char = game.Workspace:FindFirstChild(player.Name)
local RS = game.ReplicatedStorage
local StatsFolder = game.ReplicatedStorage:WaitForChild("StatsFolder")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local punchEvent = Instance.new("RemoteEvent", ReplicatedStorage)
punchEvent.Name = "PunchEvent"

local animations = {1083459730, 1083486387} --Must be changed because the animations made by me won't work for other people than me. Make your own and insert the id's here, separated by commas.

local function onPunchFired(player)
	if RS.StatsFolder:FindFirstChild(char.Name).Fat.Value > 10 or RS.StatsFolder:FindFirstChild(char.Name).Muscle.Value > 10 then
	local humanoid = char.Humanoid
	local animation = Instance.new("Animation")
	local picked = math.random(1, #animations)
	animation.AnimationId = "http://roblox.com/asset/?id="..animations[picked]
	local animTrack = humanoid:LoadAnimation(animation)
	animTrack:Play()
	local dmgScript = script.DmgScript:Clone()
	if picked == 1 then
		dmgScript.Parent = char.RightHand
	elseif picked == 2 then
		dmgScript.Parent = char.LeftHand
	end
	dmgScript.Disabled = false
	wait(0.4)
	dmgScript:Destroy()
	end
	end

punchEvent.OnServerEvent:Connect(onPunchFired)
---------------

10 comments

0

You know, you can't dump us in a random city and expect us to know our way around. Guess what, you can't go give someone your code and leave and expect them to know what it does!

0

well im sorry, thisis my first time using this website and asking for help, so please explain to me, in a professional manner, how to improve this question so I don not make the same mistake i created again.

0

i did.........................

0

Ok there, are you able to help me?

0

Imagine if the character hasn't loaded yet......

0

ok i understand now, do you know how to make the script wait for the character to load?

0

Instead of doing game.Workspace:FindFirstChild(player.Name), just simply do player.Character

0

thank you so much!!! D?mo arigat?gozaimashita!

1

I noticed that you're trying to use OnServerEvent with LocalPlayer. You cannot use OnServerEvent on a LocalScript, and you can't use LocalPlayer on a regular (server) script. You're going to be in for a surprise when you test this in an actual server.

1 answer

✓ Accepted answer

Your error is coming about because the player's character hasn't loaded yet. One simple solution is to continue to wait until the player has a character:

local char = player.Character
while not char do
	wait()
	char = player.Character
end

Other notes:

  • You should discard the first math.random() after initializing the randomseed
  • You assign ReplicatedStorage to 2 different local variables (you only need one)

1 comment

2

local char = plr.Character or plr.CharacterAdded:Wait()

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