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

attempt to call upvalue (a table value) how do i fix this?

Asked by
3wdo 198
4 years ago

so im making a game based off a game show called the death pit where you answer questions and if you dont answer them correctly you will fall into a pit. im trying to make it so its picks a random platform then asks a random question. can someone help?

local random = math.random(1, 3)
local qtable = {}
qtable[1] = "How do you say Apple in spanish"
qtable[2] = "In the rubiks cube wca, What shape is the megaminx"
qtable[3] = "What's Ikon's most liked song"
game.ReplicatedStorage.starting.OnClientEvent:Connect(function(player)
    print("Client Fired")
    wait(10)
    if random == 1 then 
        script.Parent.Text = "Platform 1, "..qtable(math.random(1,3)).."?"
        script.Parent.Platform.Value = 1
        if random == 2 then
            script.Parent.Text = "Platform 2, "..qtable(math.random(1,3)).."?"
            script.Parent.Platform.Value = 2
            if random == 3 then
            script.Parent.Text = "Platform 3, "..qtable(math.random(1,3)).."?"
            script.Parent.Platform.Value = 3
            end
        end
    end
end)

1 answer

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

It isn't working because of the way you've set up your if statements. Currently, you have them nested inside each other like this:

if random == 1 then
    if random == 2 then
        if random == 3 then

        end
    end
end 

Because of this, none of the code will run unless random == 1. What you need to do instead is include elseif statements like this:

if random == 1 then
    --code
elseif random == 2 then
    --code
elseif random == 3 then
    --code
end

This way it will check if it's 1, if it isn't 1 it will check if it's 2, if it's not 2 it will check if it's 3, if it's not 3 it won't do anything.

Additionally to fix your specific question, you want to do qtable[math.random(1, 3)] with the square brackets and not the parentheses.

EDIT: Also, if you'd like a different random value every time you need to bring the math.random variable inside of the OnClientEvent function otherwise it won't get a new random number every time.

EDIT2: Here's the script, I haven't tested it so it may not work but you can try it.

local qtable = {}
qtable[1] = "How do you say Apple in spanish"
qtable[2] = "In the rubiks cube wca, What shape is the megaminx"
qtable[3] = "What's Ikon's most liked song"
game.ReplicatedStorage.starting.OnClientEvent:Connect(function(player)
    local random = math.random(1, 3)
    print("Client Fired")
    wait(10)
    if random == 1 then 
        script.Parent.Text = "Platform 1, "..qtable[math.random(1,3)].."?"
        script.Parent.Platform.Value = 1
    elseif random == 2 then
        script.Parent.Text = "Platform 2, "..qtable[math.random(1,3)].."?"
        script.Parent.Platform.Value = 2
    elseif random == 3 then
        script.Parent.Text = "Platform 3, "..qtable[math.random(1,3)].."?"
        script.Parent.Platform.Value = 3
    end
end)
0
can you right the script? i think i did what you were saying incorrectly because it didnt work 3wdo 198 — 4y
0
write 3wdo 198 — 4y
0
I've edited it to include an example script cowsoncows 951 — 4y
Ad

Answer this question