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

I need help on getting a card selecter to work. Please?

Asked by 8 years ago

I am trying to create a game where it displays a random poker card, but I cannot get it to work. Here is the code I am using:

c1 = game.workspace.c1.Decal
function onClicked()
    local a = math.random("1", "52")
    print(a)
    if a==1 then
        c1.Texture=rbxassetid://393533133
    elseif a==2 then
        c1.Texture=rbxassetid://171982569
    elseif a==3 then
        c1.Texture=rbxassetid://171982595
    elseif a==4 then
        c1.Texture=rbxassetid://171984344
    elseif a==5 then
        c1.Texture=rbxassetid://171984333
    elseif a==6 then
        c1.Texture=rbxassetid://171984320
    elseif a==7 then
        c1.Texture=rbxassetid://171984303
        elseif a==8 then
        c1.Texture=rbxassetid://171982720
        elseif a==9 then
        c1.Texture=rbxassetid://171982735
        elseif a==10 then
        c1.Texture=rbxassetid://171982748
        elseif a==11 then
        c1.Texture=rbxassetid://171982776
        elseif a==12 then
        c1.Texture=rbxassetid://171982807
        elseif a==13 then
        c1.Texture=rbxassetid://171982794
        elseif a==14 then
        c1.Texture=rbxassetid://171982338
        elseif a==15 then
        c1.Texture=rbxassetid://171982229
        elseif a==16 then
        c1.Texture=rbxassetid://171982240
        elseif a==17 then
        c1.Texture=rbxassetid://171982250
        elseif a==18 then
        c1.Texture=rbxassetid://171982260
        elseif a==19 then
        c1.Texture=rbxassetid://171982268
        elseif a==20 then
        c1.Texture=rbxassetid://171982281
        elseif a==21 then
        c1.Texture=rbxassetid://171982292
        elseif a==22 then
        c1.Texture=rbxassetid://171982315
        elseif a==23 then
        c1.Texture=rbxassetid://171982324
        elseif a==24 then
        c1.Texture=rbxassetid://171982376
        elseif a==25 then
        c1.Texture=rbxassetid://171982420
        elseif a==26 then
        c1.Texture=rbxassetid://171982407
        elseif a==27 then
        c1.Texture=rbxassetid://171983024
        elseif a==28 then
        c1.Texture=rbxassetid://171982928
        elseif a==29 then
        c1.Texture=rbxassetid://171982942
        elseif a==30 then
        c1.Texture=rbxassetid://171982949
        elseif a==31 then
        c1.Texture=rbxassetid://171982956
        elseif a==32 then
        c1.Texture=rbxassetid://171982965
        elseif a==33 then
        c1.Texture=rbxassetid://171982976
        elseif a==34 then
        c1.Texture=rbxassetid://171982986
        elseif a==35 then
        c1.Texture=rbxassetid://171983007
        elseif a==36 then
        c1.Texture=rbxassetid://171983010
        elseif a==37 then
        c1.Texture=rbxassetid://171983031
        elseif a==38 then
        c1.Texture=rbxassetid://171983046
        elseif a==39 then
        c1.Texture=rbxassetid://171983040
        elseif a==40 then
        c1.Texture=rbxassetid://171982129
        elseif a==41 then
        c1.Texture=rbxassetid://171981991
        elseif a==42 then
        c1.Texture=rbxassetid://171982041
        elseif a==43 then
        c1.Texture=rbxassetid://171982049
        elseif a==44 then
        c1.Texture=rbxassetid://171982055
        elseif a==45 then
        c1.Texture=rbxassetid://171982068
        elseif a==46 then
        c1.Texture=rbxassetid://171982074
        elseif a==47 then
        c1.Texture=rbxassetid://171982085
        elseif a==48 then
        c1.Texture=rbxassetid://171982096
        elseif a==49 then
        c1.Texture=rbxassetid://171982119
        elseif a==50 then
        c1.Texture=rbxassetid://171982151
        elseif a==51 then
        c1.Texture=rbxassetid://171982169
        elseif a==52 then
        c1.Texture=rbxassetid://171982157
    end
end

game.Workspace.Reset.ClickDetector.MouseClick:connect(onClicked)

I get the error (6,25) Expected identifier, got '/' in Script Analysis, as well as error Workspace.Reset.Script:6: '<name>' expected near '/' in output.

0
It needs to be a string, .Texture = "rbxassetid://1234", also, try using tables and iterations next time,it's easier. 1N0body 206 — 8y

1 answer

Log in to vote
0
Answered by 8 years ago

Building upon 1N0body comment there are two general rules of programming which needs to be looked at.

"Duplicate code" - a big giveaway that your design/method of codeing needs to be changed, we write code once and only once. Then we can re-use it, this makes our code much more maintainable in the long run. There may be exceptions to this.

"Maintainability" - with this current design of code it would take a long time to add to e.g. in the middle of your if statements as well as you have fixed variables for something which may change.

optimize code

local c1 = game.workspace.c1.Decal

-- just make a list so we dont need have lots of elseif 
local textureList = {
    'rbxassetid://393533133','rbxassetid://171982569','rbxassetid://171982595','rbxassetid://171984344',
    'rbxassetid://171984333','rbxassetid://171984320','rbxassetid://171984303','rbxassetid://171982720',
    'rbxassetid://171982735','rbxassetid://171982748','rbxassetid://171982776','rbxassetid://171982807',
    'rbxassetid://171982794','rbxassetid://171982338','rbxassetid://171982229','rbxassetid://171982240',
    'rbxassetid://171982250','rbxassetid://171982260','rbxassetid://171982268','rbxassetid://171982281',
    'rbxassetid://171982292','rbxassetid://171982315','rbxassetid://171982324','rbxassetid://171982376',
    'rbxassetid://171982420','rbxassetid://171982407','rbxassetid://171983024','rbxassetid://171982928',
    'rbxassetid://171982942','rbxassetid://171982949','rbxassetid://171982956','rbxassetid://171982965',
    'rbxassetid://171982976','rbxassetid://171982986','rbxassetid://171983007','rbxassetid://171983010',
    'rbxassetid://171983031','rbxassetid://171983046','rbxassetid://171983040','rbxassetid://171982129',
    'rbxassetid://171981991','rbxassetid://171982041','rbxassetid://171982049','rbxassetid://171982055',
    'rbxassetid://171982068','rbxassetid://171982074','rbxassetid://171982085','rbxassetid://171982096',
    'rbxassetid://171982119','rbxassetid://171982151','rbxassetid://171982169','rbxassetid://171982157'
}

function onClicked()
    local num = math.random(1, #textureList) -- the #  is used to get then number of elements within the table
    --this means that we do not need to change this code when we add or remove from the list

    print(num)
    c1.Texture=textureList[num] -- instead of using the long if statement we can simply pick the index from out pre made table
    -- this save both processing time and allows us to easily add to the list
end

game.Workspace.Reset.ClickDetector.MouseClick:connect(onClicked)

Ad

Answer this question