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

Global variables not storing inside of a function?

Asked by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
8 years ago

I was having an issue with my variable, so I looked into it and then changed them to global variables, rather than local. Even after doing this, the variables don't store, or change at all.

What I'm trying to do is make it so that when the player clicks a button, and then clicks done, it stores that player's choice after reset, when a different button is pressed. When I print the variable right after clicking the button, it still returns nil. How do I store this variable?

The relevant parts of script:

-- [[ Player ]]
local player = game.Players.LocalPlayer
repeat wait() until player.Character
playercolor = " " -- The variable I'm trying to store.
player.Character.Humanoid.WalkSpeed = 0

-- [[ Title Screen ]]
local titlescreen = Instance.new("ScreenGui", script.Parent)
titlescreen.Name = "Title Screen"

-- New Game
titlebutton = Instance.new("TextButton", titlescreen)
titlebutton.Name = "New Game"
titlebutton.Position = UDim2.new(0.4, 0, 0.35, 0)
titlebutton.Size = UDim2.new(0.25, 0, 0.1, 0)
titlebutton.BackgroundColor3 = Color3.new(144, 0, 0)
titlebutton.FontSize = "Size14"
titlebutton.Text = "New Game"

-- Continue Game
titlebutton2 = Instance.new("TextButton", titlescreen)
titlebutton2.Name = "Continue Game"
titlebutton2.Position = UDim2.new(0.4, 0, 0.55, 0)
titlebutton2.Size = UDim2.new(0.25, 0, 0.1, 0)
titlebutton2.BackgroundColor3 = Color3.new(144, 0, 0)
titlebutton2.FontSize = "Size14"
titlebutton2.Text = "Continue Game"


-- [[ Character Creation ]]
local CharacterCreate = Instance.new("ScreenGui", script.Parent)
CharacterCreate.Name = "Character Creation"

local CharacterCreateDone = Instance.new("TextButton", CharacterCreate)
CharacterCreateDone.Name = "Done"
CharacterCreateDone.Position = UDim2.new(0.9, 0, 0.9, 0)
CharacterCreateDone.Size = UDim2.new(0.08, 0, 0.05, 0)
CharacterCreateDone.BackgroundColor3 = Color3.new(144, 0, 0)
CharacterCreateDone.FontSize = "Size12"
CharacterCreateDone.Text = "Done"
CharacterCreateDone.Visible = false


-- [ Body Colors ]

-- Color Definitions
local bc1 = BrickColor.new("Dark orange")
local bc2 = BrickColor.new("Reddish brown")
local bc3 = BrickColor.new("Pastel brown")
local c1 = bc1.Color
local c2 = bc2.Color
local c3 = bc3.Color

-- Buttons
local cc1 = Instance.new("TextButton", CharacterCreate)
cc1.Visible = false
cc1.Name = "Dark Orange"
cc1.Position = UDim2.new(0.04, 0, 0.35, 0)
cc1.Size = UDim2.new(0.02, 0 , 0.04, 0)
cc1.BackgroundColor3 = c1
cc1.Text = " "

local cc2 = Instance.new("TextButton", CharacterCreate)
cc2.Visible = false
cc2.Name = "Reddish Brown"
cc2.Position = UDim2.new(0.08, 0, 0.35, 0)
cc2.Size = UDim2.new(0.02, 0 , 0.04, 0)
cc2.BackgroundColor3 = c2
cc2.Text = " "

local cc3 = Instance.new("TextButton", CharacterCreate)
cc3.Visible = false
cc3.Name = "Pastel Brown"
cc3.Position = UDim2.new(0.12, 0, 0.35, 0)
cc3.Size = UDim2.new(0.02, 0 , 0.04, 0)
cc3.BackgroundColor3 = c3
cc3.Text = " "



-- Camera
local CameraLocation2 = Vector3.new(-29, 8, -95)
local CameraDirection2 = Vector3.new(-29.015, 8.004, -72.534)
local charactercam = false


-- [[ MouseClicks ]]

-- [ TitleButton MouseClicks ]

function TB2click() --This is the function which should restore whichever choice the player made.
    titlescreen:Destroy()
    titlecam = false
    cam.CameraSubject = player.Character.Humanoid
    cam.CameraType = "Custom"
    player.Character.Humanoid.WalkSpeed = 16

    if playercolor == "Pastel brown" then
        player.Character.Head.BrickColor = BrickColor.new("Pastel brown")
        player.Character.Torso.BrickColor = BrickColor.new("Pastel brown")
        player.Character["Right Arm"].BrickColor = BrickColor.new("Pastel brown")
        player.Character["Left Arm"].BrickColor = BrickColor.new("Pastel brown")
        player.Character["Right Leg"].BrickColor = BrickColor.new("Pastel brown")
        player.Character["Left Leg"].BrickColor = BrickColor.new("Pastel brown")

    elseif playercolor == "Reddish brown" then
        player.Character.Head.BrickColor = BrickColor.new("Reddish brown")
        player.Character.Torso.BrickColor = BrickColor.new("Reddish brown")
        player.Character["Right Arm"].BrickColor = BrickColor.new("Reddish brown")
        player.Character["Left Arm"].BrickColor = BrickColor.new("Reddish brown")
        player.Character["Right Leg"].BrickColor = BrickColor.new("Reddish brown")
        player.Character["Left Leg"].BrickColor = BrickColor.new("Reddish brown")

    elseif playercolor == "Dark orange" then
        player.Character.Head.BrickColor = BrickColor.new("Dark orange")
        player.Character.Torso.BrickColor = BrickColor.new("Dark orange")
        player.Character["Right Arm"].BrickColor = BrickColor.new("Dark orange")
        player.Character["Left Arm"].BrickColor = BrickColor.new("Dark orange")
        player.Character["Right Leg"].BrickColor = BrickColor.new("Dark orange")
        player.Character["Left Leg"].BrickColor = BrickColor.new("Dark orange")

    else
        player.Character.Head.BrickColor = BrickColor.new("Pastel brown")
        player.Character.Torso.BrickColor = BrickColor.new("Pastel brown")
        player.Character["Right Arm"].BrickColor = BrickColor.new("Pastel brown")
        player.Character["Left Arm"].BrickColor = BrickColor.new("Pastel brown")
        player.Character["Right Leg"].BrickColor = BrickColor.new("Pastel brown")
        player.Character["Left Leg"].BrickColor = BrickColor.new("Pastel brown")
    end
end


-- [ Body Color MouseClicks ] -- This is where the player defines the playercolor.
function c1Click()
    cc2.BorderColor3 = Color3.new(0, 0, 0)
    cc2.BorderSizePixel = 1
    cc3.BorderColor3 = Color3.new(0, 0, 0)
    cc3.BorderSizePixel = 1

    cc1.BorderColor3 = Color3.new(255, 255, 255)
    cc1.BorderSizePixel = 2
    player.Character.Head.BrickColor = BrickColor.new("Dark orange")
    player.Character.Torso.BrickColor = BrickColor.new("Dark orange")
    player.Character["Right Arm"].BrickColor = BrickColor.new("Dark orange")
    player.Character["Left Arm"].BrickColor = BrickColor.new("Dark orange")
    player.Character["Right Leg"].BrickColor = BrickColor.new("Dark orange")
    player.Character["Left Leg"].BrickColor = BrickColor.new("Dark orange")

    playercolor = "Dark orange"
end
function c2Click()
    cc1.BorderColor3 = Color3.new(0, 0, 0)
    cc1.BorderSizePixel = 1
    cc3.BorderColor3 = Color3.new(0, 0, 0)
    cc3.BorderSizePixel = 1

    cc2.BorderColor3 = Color3.new(255, 255, 255)
    cc2.BorderSizePixel = 2
    player.Character.Head.BrickColor = BrickColor.new("Reddish brown")
    player.Character.Torso.BrickColor = BrickColor.new("Reddish brown")
    player.Character["Right Arm"].BrickColor = BrickColor.new("Reddish brown")
    player.Character["Left Arm"].BrickColor = BrickColor.new("Reddish brown")
    player.Character["Right Leg"].BrickColor = BrickColor.new("Reddish brown")
    player.Character["Left Leg"].BrickColor = BrickColor.new("Reddish brown")

    playercolor = "Reddish brown"
end
function c3Click()
    cc1.BorderColor3 = Color3.new(0, 0, 0)
    cc1.BorderSizePixel = 1
    cc2.BorderColor3 = Color3.new(0, 0, 0)
    cc2.BorderSizePixel = 1

    cc3.BorderColor3 = Color3.new(255, 255, 255)
    cc3.BorderSizePixel = 2
    player.Character.Head.BrickColor = BrickColor.new("Pastel brown")
    player.Character.Torso.BrickColor = BrickColor.new("Pastel brown")
    player.Character["Right Arm"].BrickColor = BrickColor.new("Pastel brown")
    player.Character["Left Arm"].BrickColor = BrickColor.new("Pastel brown")
    player.Character["Right Leg"].BrickColor = BrickColor.new("Pastel brown")
    player.Character["Left Leg"].BrickColor = BrickColor.new("Pastel brown")

    playercolor = "Pastel brown"
end
function ccD()
    CharacterCreate:Destroy()
    charactercam = false

    cam.CameraSubject = player.Character.Humanoid
    cam.CameraType = "Custom"
    player.Character.Humanoid.WalkSpeed = 16
end



-- MouseClick Connectors 
titlebutton.MouseButton1Down:connect(TB1click)
titlebutton2.MouseButton1Down:connect(TB2click)
cc1.MouseButton1Down:connect(c1Click)
cc2.MouseButton1Down:connect(c2Click)
cc3.MouseButton1Down:connect(c3Click)
CharacterCreateDone.MouseButton1Down:connect(ccD)

Help would be appreciated, and thanks in advance!

0
but you don't print the variable right after clicking the button. 1waffle1 2908 — 8y
0
I used the command bar. Pyrondon 2089 — 8y
0
this script and the command bar are two entirely separate universes 1waffle1 2908 — 8y
0
Oh, I was unaware. I had tried it in the script prior and it still printed nil. Is there a reason the variables aren't working as intended? Pyrondon 2089 — 8y
View all comments (5 more)
0
you didn't do what you say you did. I know that because you're saying it printed nil. if you wrote "print(playercolor)" anywhere after line 4, it would not print nil. whatever you did wasn't that. 1waffle1 2908 — 8y
0
I tried it at 149, 166, and 183. What you're saying is that the script should work fine? Pressing TB2Click button always made the color pastel brown, no matter what I did. Pyrondon 2089 — 8y
0
write print(playercolor) on line 97 1waffle1 2908 — 8y
0
Alright, when I can access it again I will. Thanks. Pyrondon 2089 — 8y
0
When I printed at line 97, it returned nil. This is in a LocalScript in StarterGui, do I need to post the full script? Pyrondon 2089 — 8y

Answer this question