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

How come my script doesn't work a second time, and how come it doesn't move back 0.2 and back?

Asked by 8 years ago

I made a script where I click the button and it supposed to move back 0.2 of the z axis which mean the music should be on, and if I click it again it is supposed to come back to normal as in come back adding 0.2 and then the music stops. It also determines to turn on if the decal is in the id I put in. Well, the problem is that I tested it and it worked on the first time, but it didn't move back or anything, and it also didn't start the sound after the second time. This is the script:

button = game.Workspace.Buttons.Button1
disc = game.Workspace.PlainDisc3.Decal
sound = Instance.new("Sound", disc)
radioOn = false

function onClicked()
    if not radioOn and disc.Texture == "rbxassetid://238387775" then
        radioOn = true
        sound.SoundId = "rbxassetid://210232032"
        sound:Play()
    button.Position=button.Position-Vector3.new(0, 0, -0.2)
    if radioOn and sound.SoundId == "rbxassetid://210232032" then
    button.Position=button.Position+Vector3.new(0, 0, -0.2)
            sound:Stop()
            radioOn = false
        end
    end
end

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

I hope you understand

1 answer

Log in to vote
1
Answered by
yoshi8080 445 Moderation Voter
8 years ago

I fixed it, it was very simple. The errors I put in the script

button = script.Parent.Parent.Button1
disc =  script.Parent.Parent.Parent.PlainDisc3.Decal
discpart = script.Parent.Parent.Parent.PlainDisc3
sound = Instance.new("Sound", discpart) -- you placed the audio in the decal which would be better placed in the disk
radioOn = false

function onClicked()
    if not radioOn and disc.Texture == "rbxassetid://238387775" then
        radioOn = true
        sound.SoundId = "rbxassetid://210232032"
        sound:Play()
    button.CFrame = button.CFrame.new(0,-0.2,0) -- Use c-frame not position
    wait()
else -- use else to make the sound be able to turn off and on
    if radioOn and sound.SoundId == "rbxassetid://210232032" then
    button.CFrame = button.CFrame.new(0,0.2,0)
            sound:Stop()
            radioOn = false
        end
    end
end

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

Answer this question