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

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

Asked by 7 years ago
Edited 7 years ago

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)
---------------
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! hiimgoodpack 2009 — 7y
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. taunter165 30 — 7y
0
i did......................... hiimgoodpack 2009 — 7y
0
Ok there, are you able to help me? taunter165 30 — 7y
View all comments (6 more)
0
Imagine if the character hasn't loaded yet...... hiimgoodpack 2009 — 7y
0
ok i understand now, do you know how to make the script wait for the character to load? taunter165 30 — 7y
0
Instead of doing game.Workspace:FindFirstChild(player.Name), just simply do player.Character saSlol2436 716 — 7y
0
thank you so much!!! D?mo arigat?gozaimashita! taunter165 30 — 7y
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. XAXA 1569 — 7y

Closed as Non-Descriptive by hiimgoodpack, XAXA, abnotaddable, chess123mate, Thundermaker300, and Goulstem

This question has been closed because its title or content does not adequately describe the problem you are trying to solve.
Please ensure that your question pertains to your actual problem, rather than your attempted solution. That is, you were trying to solve problem X, and you thought solution Y would work, but instead of asking about X when you ran into trouble, you asked about Y.

Why was this question closed?

1 answer

Log in to vote
1
Answered by 7 years ago

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)
2
local char = plr.Character or plr.CharacterAdded:Wait() CootKitty 311 — 7y
Ad