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

BoolValue and 'If Then' help?

Asked by 10 years ago

(I'm asking so many questions today)

So I have a toaster, and my game is supposed to be very realistic, so When you unplug the toaster you can't power the toaster.

So I have a BoolValue called "Plugged" in the ToasterPug, and when it's pulled, you shouldn't be able to power the toaster again. Well this doesn't work.

What DOES work is inside of the Plug, there is a part of the script that prevents you from pulling the plug when it's already pulled, and that works.

Here's the script to the Plug. (the one that works)

Plugged = script.Parent.Parent:WaitForChild("Plugged").Value
Cord = script.Parent.Parent.Cord

function onClicked(playerWhoClicked)
    if Plugged == true then
    Plugged = false
    script.Parent.CFrame = script.Parent.CFrame * CFrame.new(0, 1, 0)
    Cord.CFrame = Cord.CFrame * CFrame.new(0, 1, 0)
    script.Parent.Parent.Plugged.Value = false
    script.Parent.Sparkles.Enabled = true
    wait(0.5)
    script.Parent.Sparkles.Enabled = false
    workspace.Toaster.Base.Fire.Enabled = false
    workspace.Toaster.Base.Smoke.Enabled = false
    workspace.Toaster.Base.PointLight.Enabled = false
    wait(5)
    workspace.Toaster.Heat1.BrickColor = BrickColor.new("Medium stone grey")
    workspace.Toaster.Heat2.BrickColor = BrickColor.new("Medium stone grey")
    else return
        end

end


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

And here's the script inside the Toaster Switch

Plugged = workspace.ToasterCord:WaitForChild("Plugged").Value


function onClicked(playerWhoClicked)
    if Plugged == true then
    script.Parent.CFrame = script.Parent.CFrame * CFrame.new(0, -0.4, 0)
    wait(0.7)
    script.Parent.CFrame = script.Parent.CFrame * CFrame.new(0, 0.4, 0)
    wait(6)
    workspace.Toaster.Heat1.BrickColor = BrickColor.new("Dusty Rose")
    workspace.Toaster.Heat2.BrickColor = BrickColor.new("Dusty Rose")
    wait(6)
    workspace.Toaster.Heat1.BrickColor = BrickColor.Red()
    workspace.Toaster.Heat2.BrickColor = BrickColor.Red()
    wait(8)
    workspace.Toaster.Heat1.BrickColor = BrickColor.new("Really red")
    workspace.Toaster.Heat2.BrickColor = BrickColor.new("Really red")
    wait(10)
    script.Parent.Parent.Base.Smoke.Enabled = true
    wait(12)
    script.Parent.Parent.Base.Smoke.Opacity = 0.3
    wait(30)
    script.Parent.Parent.Base.Smoke.Opacity = 1
    wait(10)
    script.Parent.Parent.Base.Fire.Enabled = true
    script.Parent.Parent.Base.PointLight.Enabled = true
    if Plugged == false then return

    end



end
end

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

1 answer

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

If you were to inspect your Plugged BoolValue, then you'd see its value never changes.

That's because you never assigned to its property .Value.

You instead assign to the local variable Plugged.

Your assignments on line 1 in each script in no way "link" the value of the BoolValue with the variable Plugged.

It just looks at the current value (whatever the BoolValue starts as) and remembers either true or false from there.


The first script works because you're accomplishing the same thing -- it's the only thing that changes the value, and it reads from the value that it's changing.

The second one doesn't work because the first script isn't actually changing the BoolValue.


Instead of making Plugged refer to the value, make Plugged refer to the object and ask for Plugged.Value wherever you had Plugged:

E.G.:

function onClicked(playerWhoClicked)
    if Plugged.Value == true then
        Plugged.Value = false
        script.Parent.CFrame = script.Parent.CFrame * CFrame.new(0, 1, 0)
        Cord.CFrame = Cord.CFrame * CFrame.new(0, 1, 0)
        script.Parent.Parent.Plugged.Value = false
        script.Parent.Sparkles.Enabled = true
        wait(0.5)
        script.Parent.Sparkles.Enabled = false
        workspace.Toaster.Base.Fire.Enabled = false
        workspace.Toaster.Base.Smoke.Enabled = false
        workspace.Toaster.Base.PointLight.Enabled = false
        wait(5)
        workspace.Toaster.Heat1.BrickColor = BrickColor.new("Medium stone grey")
        workspace.Toaster.Heat2.BrickColor = BrickColor.new("Medium stone grey")
    else
        return
    end
end

Also you should consider tabbing correctly as in the above.

(Do the same for the other script too)

0
Thank you! I've been learning so much more about Roblox Lua ever since I joined Scripting Helpers! SpazzMan502 133 — 10y
Ad

Answer this question