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

What is wrong with this decal-changing script?

Asked by
iAviate 20
9 years ago

Hi, So i'm writing this script that changes the decal texture asset ID of a part called "Screen". I've made loads of different stringvalues (one, two, three) which each have their own asset ID link (eg. http://www.roblox.com/asset/?id=238110849) i'm quite sure the links are ok. The hierarchy is:

Model, one, two, three, four, five, six, seven, eight, Part (ClickDetector, Script), Sign (PointLight, Decal)

Here's the script:

isOn = 0
local a=script.Parent.Parent.one.Value
local b=script.Parent.Parent.two.Value
local c=script.Parent.Parent.three.Value
local d=script.Parent.Parent.four.Value
local e=script.Parent.Parent.five.Value
local f=script.Parent.Parent.six.Value
local g=script.Parent.Parent.seven.Value
local h=script.Parent.Parent.eight.Value
local sign=script.Parent.Parent.Sign.Decal.Texture

function three()
do sign=""..a
    isOn=2
end
end

function four()
sign=""..b
    isOn=3
end

function five()
sign=""..c
    isOn=4
end

function six()
sign=""..d
    isOn=5
end

function seven()
sign=""..e
    isOn=6
end

function eight()
sign=""..f
    isOn=7
end

function two()
sign=""..g
    isOn=1
end

function nine()
sign=""..h
    isOn=0
end

function onClicked()


    if isOn == 0 then two() elseif isOn==1 then three() elseif isOn==2 then four() elseif isOn==3 then five() elseif isOn==4 then six() elseif isOn==5 then seven() elseif isOn==6 then eight() else nine() end

end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

nine()

I know it's unnecessarily long but I made the original a while ago and just edited that.

0
There's nothing in the output/script analysis... iAviate 20 — 9y

3 answers

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

You change the variable sign, but only the variable sign.

All that does is overwrite sign with the new texture. That doesn't change the texture of script.Parent.Parent.Sign.Decal.

You have to explicitly change a property to change the property:

local decal = script.Parent.Parent.Sign.Decal

......

decal.Texture = f -- or whatever else here

Let's fix this code, though. Instead of having a bunch of variables, make a list:

local list = {
    script.Parent.Parent.one.Value,
    script.Parent.Parent.two.Value,
    script.Parent.Parent.three.Value,
    script.Parent.Parent.four.Value,
    script.Parent.Parent.five.Value,
    script.Parent.Parent.six.Value,
    script.Parent.Parent.seven.Value,
    script.Parent.Parent.eight.Value
}

Now it's easy to do the rest:

local shown = 1

-- Advance to the next one
function advance()
    shown = shown + 1 -- Got to the next one
    if shown > #list then
        -- I'm past the end -- start over at 1
        shown = 1
    end
    -- (or you can fiddle with mods to get this to one line of code)
end

-- Show the currently selected slide
function show()
    decal.Texture = list[shown]
end

show() -- Show the first one

function onClicked()
    advance()
    show()
end
Ad
Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

Your problem is that you're attempting to edit a property directly from a variable

This won't work, you have to make the variable the object that the property is tied to, then edit the variable by indexing the property.

Also, your script has so many unnecessary aspects to it. Just make a table containing all of the values, then use your int variable to index the table.

local num =  0
local sign = script.Parent.Parent.Sign.Decal
local vals = { --Table full of all the values
script.Parent.Parent.one,
script.Parent.Parent.two,
script.Parent.Parent.three,
script.Parent.Parent.four,
script.Parent.Parent.five,
script.Parent.Parent.six,
script.Parent.Parent.seven,
script.Parent.Parent.eight
}

function incrementDecal()
    if num == #vals + 1 then num = 0 end --reset if it's at limit
    num = num + 1 --increment
    sign.Texture = vals[num].Value --Set the texture
end

script.Parent.ClickDetector.MouseClick:connect(function()
    incrementDecal()
end)
0
BlueTaslem keeps ninja'ing me. Goulstem 8144 — 9y
Log in to vote
0
Answered by 9 years ago

Your script is just over-complicated, which makes it easier to get errors. It would be much simpler to store all the string values into a table, and then having a variable that gets incremented by 1 every time the button is clicked. That way, you can use that variable to get the corresponding element in the table. Like so:

local Index = 1 --This is the index variable. It will be used to get the corresponding element from the table below
local Textures = { --This table contains the textureId's for the decals. You can also replace the "script.Parent.Parent.one.Value" with the actual value of One, and do the same for the rest of them
    script.Parent.Parent.one.Value;
    script.Parent.Parent.two.Value;
    script.Parent.Parent.three.Value;
    script.Parent.Parent.four.Value;
    script.Parent.Parent.five.Value;
    script.Parent.Parent.six.Value;
    script.Parent.Parent.seven.Value;
    script.Parent.Parent.eight.Value;
}
local Sign=script.Parent.Parent.Sign.Decal --The sign variable has to be the decal, not the texture of the decal because the texture of the decal is what you're trying to change. If Sign was the decal's texture, then whenever you said "Sign = ...", that would change the variable, not the texture

function onClicked()
    Sign.Texture = Textures[Index] --This sets the textureId of the decal to the Index'th element of the table "Textures"
    if Index == #Textures then --If the index value is the length of the table, then reset the Index value back to 1
        Index = 1
    else --If it's not, then just increment the Index value by 1
        Index = Index + 1
    end
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

Hope this helped!

0
Thanks so much! iAviate 20 — 9y

Answer this question