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

My local script is getting an error for my character variable when I play it in a server ?

Asked by 5 years ago

this is the error: Players.Jacobingg.Backpack.Stand.carrot2:9: attempt to index upvalue 'character' (a nil value)

It's suppose to send the unequip remote event when the player presses q (yes i accidentally swapped the two in my server script so im just doing that.), also this error started happening when i converted my tool script to just a script that activates when the player presses q.

I was also going to post the server script, but it's too long to post, so i'm not so sure what to do about that, i'm hoping the error just includes the local script.

--local Tool = script.Parent
local play = script.Parent.Parent.Parent
local character = play.Character
local Mouse = play:GetMouse()



Mouse.KeyDown:connect(function(key)
    if(key == "q")and not character:FindFirstChild("Stand")  then
script.Parent.unEquip:FireServer()

elseif (key == "q")and character:FindFirstChild("Stand")  then
    script.Parent.Equip:FireServer()
    end
end)


Mouse.Button1Down:connect(function()
    script.Parent.clicc:FireServer()
end)
local stand =   character:WaitForChild('Stand')
local StandHead = stand:findFirstChild("Head")
local StandTorso = stand:findFirstChild("Torso")
local StandHR = stand:findFirstChild("HumanoidRootPart")
local StandRA = stand["Right Arm"]
local StandLA = stand["Left Arm"]
local StandRL = stand["Right Leg"]
local StandLL = stand["Left Leg"]
game:GetService("RunService").Stepped:connect(function()

                StandHead.CanCollide = false 
            StandTorso.CanCollide = false 
            end)

local color = BrickColor.random()
    StandHead.BrickColor = color
    StandTorso.BrickColor = color
StandLA.BrickColor = color
StandLL.BrickColor = color
StandRA.BrickColor = color
StandRL.BrickColor = color
1
KeyDown is deprecated, use UserInputService instead. Also since this would have to be a LocalScript, use "local player = game.Players.LocalPlayer" and "local character = player.Character or player.CharacterAdded:Wait()". Also use :Connect as :connect is deprecated. MythicalShade 420 — 5y
0
What is "play'? Is it the player? Pojoto 329 — 5y
0
Your issue is that the character loads in after the script runs, so use the character variable I said. MythicalShade 420 — 5y
0
Thanks! Jacobingg 2 — 5y
View all comments (4 more)
0
Also... doing "script.Parent.Equip:FireServer()" does nothing as "Equipped" and "Unequipped" are events not properties. Use "script.Parent.Equipped:Connect(function()" instead and then fire the server. MythicalShade 420 — 5y
0
Thanks, one of those fixes worked, I didn't realize i was using so many deprecated things. Jacobingg 2 — 5y
0
No worries MythicalShade 420 — 5y
0
No worries MythicalShade 420 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

This is happening because by the time the script ran, the character was not loaded. You'll need to compensate for this, and you can do this by using the CharacterAdded event. This will yield until the character has been added and return the character.

--local Tool = script.Parent
local play = game.Players.LocalPlayer
local character = play.Character or play.CharacterAdded:Wait() -- wait for char
local uis = game:GetService("UserInputService") -- KeyDown is deprecated 



uis.InputBegan:Connect(function(key, gpe)
    if gpe then return end--if they're chatting end the function 

    if key.KeyCode == Enum.KeyCode.Q and not character:FindFirstChild("Stand")  then
        script.Parent.unEquip:FireServer()

    elseif character:FindFirstChild("Stand") then
        script.Parent.Equip:FireServer()
    end
end)


uis.InputBegan:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        -- if mouse button 1 pressed...
        script.Parent.clicc:FireServer()
    end
end)

local stand = character:WaitForChild('Stand') 
local StandHead = stand:FindFirstChild("Head")--findFirstChild deprecated
local StandTorso = stand:FindFirstChild("Torso")
local StandHR = stand:FindFirstChild("HumanoidRootPart")
local StandRA = stand["Right Arm"]
local StandLA = stand["Left Arm"]
local StandRL = stand["Right Leg"]
local StandLL = stand["Left Leg"]

game:GetService("RunService").Stepped:Connect(function()
    StandHead.CanCollide = false 
    StandTorso.CanCollide = false 
end)

local color = BrickColor.Random()--random deprecated 
StandHead.BrickColor = color
StandTorso.BrickColor = color
StandLA.BrickColor = color
StandLL.BrickColor = color
StandRA.BrickColor = color
StandRL.BrickColor = color

Ad

Answer this question