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' fails?

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

✓ Accepted answer

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)

1 comment

0

Thanks! this really helped. i'll try to use :WatForChild() more often.

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