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

attempt to index upvalue 'Character' fails?

Asked by 5 years ago
local UserInputService = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Mouse = Player:GetMouse()

local PunchCooldown = 0.5
local PunchReady = true

Mouse.Button1Down:Connect(function()
    if PunchReady then
        Character.Punchleft:Play()
    end
end)

I know the location of the player's character. I am using a custom character with all the proper r15 joints in place. i cant put my finger down as to why it's not working.

i don't really know what else of other information to put here. if you need some data from my game to try and fix this, i could edit this post.

1 answer

Log in to vote
0
Answered by
0_2k 496 Moderation Voter
5 years ago

The character hasn't loaded yet, one of the following codes provided would work.

local UserInputService = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Mouse = Player:GetMouse()

local PunchCooldown = 0.5
local PunchReady = true

while not Character do
    wait()
    Character = Player.Character
end

Mouse.Button1Down:Connect(function()
    if PunchReady then
        Character.Punchleft:Play()
    end
end)

OR this

local UserInputService = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Character = Player:WaitForChild("Character")
local Mouse = Player:GetMouse()

local PunchCooldown = 0.5
local PunchReady = true

Mouse.Button1Down:Connect(function()
    if PunchReady then
        Character.Punchleft:Play()
    end
end)
0
Thanks! this really helped. i'll try to use :WatForChild() more often. gartfart36 5 — 5y
Ad

Answer this question