Why are my scripts not working?

I am working on two different scripts at the moment, and neither of them seem to be working, and I cannot find the issue. (I am new to scripting, so sorry if these are dumb mistakes)

[#1 SOLVED (see #2)] 1) This first script is simple. You are supposed to click a part, and then the music plays in a certain radius. This is what I have:

function onClicked()

game.Workspace.RoomMusic.SoundBlock.Sound:Play()
print "musicplaying"

end

game.Workspace.RoomMusic.Part.ClickDetector:connect(onClicked)

I feel like the problem has something to do with the ClickDetector, as it doesn’t print when clicked.
image

  1. The second thing I am scripting is a shower. The shower is already scripted to turn on/off, but, I want the shower to flood if it is left on too long. This is what I have so far:
    Shower Script:
    function onClicked()

    script.Parent.Parent.ShowerHead.ParticleEmitter.Enabled = true
    game.Workspace.Shower.ShowerOff.Transparency = 1
    game.Workspace.Shower.ShowerOff.CanCollide = false
    game.Workspace.Shower.ShowerOn.Transparency = 0
    game.Workspace.Shower.ShowerOn.CanCollide = true
    print “showeron”

    game.Workspace.ShowerSoundBlock.Sound:Play()
    game.Workspace.ShowerSoundBlock.Sound.MaxDistance = 15

    script.Parent.ClickDetector.MaxActivationDistance = 0

    script.Parent.Parent.FloodingValue.Value = 1

end

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

Flood Script:
–| If on, start flooding

if script.Parent.FloodingValue.Value == 1 then

wait(1)

script.Parent.Flood1.Transparency = 0

wait(3)

script.Parent.Flood2.Transparency = 0

wait(3)

script.Parent.Flood3.Transparency = 0

wait(3)

script.Parent.Flood4.Transparency = 0

end

–| If off, drain water

if script.Parent.FloodingValue.Value == 0 then

wait(3)

script.Parent.Flood4.Transparency = 1

wait(3)

script.Parent.Flood3.Transparency = 1

wait(3)

script.Parent.Flood2.Transparency = 1

wait(3)

script.Parent.Flood1.Transparency = 1

end

I already tested it a bit, everything prints and the IntValue does change, but the blocks (Flood1, Flood2, Flood3, Flood4) don’t become visible.
image

Again, sorry if these are stupid mistakes. :grimacing:

1 Like

For the first problem, you should put
game.Workspace.RoomMusic.Part.ClickDetector.MouseClick:connect(onClicked)
rather than
game.Workspace.RoomMusic.Part.ClickDetector:connect(onClicked)

As for the second issue, I’m not quite sure. If the parts are CanCollide off, then make sure they are anchored. If they arent anchored they will fall through the map.

Edit:
I would also look into using script.Parent and so on more, so you don’t have to keep writing the entire path

2 Likes

To add to @retroblocx

this is your click detector, an object
game.Workspace.RoomMusic.Part.ClickDetector
you need a related event to connect the function to.

at the bottom of each instance API is a list of valid events.
they can inherit events from parent classes too.

game.Workspace.RoomMusic.Part.ClickDetector.MouseClick
is your event to :Connect

2 Likes

Hello! Every object have Event you wanted to do something when someone click on clickDetector you don’t type any event this should be like this

function onClicked()
game.Workspace.RoomMusic.SoundBlock.Sound:Play()
print "musicplaying"
end

game.Workspace.RoomMusic.Part.ClickDetector.MouseClick:connect(onClicked)

MouseClick fires when someone clicks on clickDetector.

1 Like

@retroblocx @Pharyx_Styx @Diltz_3

Thank you all for your help on getting the first issue working! I dearly appreciate your help. And I double-checked, the ‘flood’ blocks are anchored, so that’s still an issue.

1 Like

could you straighten out the formatting for the shower, its hard to know whats going on. is this in just 1 script? or multiple.

Ok, so here’s what is wrong.

Your script is only checking when the game firsts runs!

To make sure it changes every time the value changes, change the script to this:

local FloodingValue = script.Parent.FloodingValue

FloodingValue.Changed:Connect(function()
    if FloodingValue.Value == 1 then
        -- make them appear
    elseif FloodingValue.Value == 0 then
        -- make them disappear
    end
end)

Awesome, thank you for all your help.

1 Like

Hi! Something to keep in mind next time you post a chunk of code, please format it properly. It makes it a lot easier to read and makes it easier for other people to help you. If you need help on how to do that, feel free to check out this post I wrote.

1 Like