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

code isn't working to make the gravity and lighting and map colour back to normal?

Asked by 4 years ago

Hello everyone, I am making a game where when you press a button something happens, there is a problem though, I have made my game so the has a 50 50 chance of going low gravity and when it does go low gravity it will make the map grey and make some parts/models visible, although most of the code worked some didn't, as you can see on line 36 I made it so it goes back to normal (the time of day and the parts disappear again and gravity goes back to normal after 12 seconds), all of the code making it turn into low gravity and using the if statement to make the parts visible worked but the if statment on line 36 making it back to normal didn't . The scripit is a normal script not a local or module, its parent is the button you press to do the 5050 chance. Hopefully this isn't just roblox and you scripters can help me out, thank you!

The script is below, line 36 is the if statement that was supposed to make it back to normal.

local lighting = game.Lighting
local button = script.Parent
local ClickDetector = Instance.new("ClickDetector")
ClickDetector.Parent = button
ClickDetector.MaxActivationDistance = 15

ClickDetector.MouseClick:Connect(function()
    print("Button has been clicked.")
    button.Transparency = 1
    game.Workspace.Stand.Transparency = 1
    game.Workspace.Stand.CanCollide = false
    button.CanCollide = false
    wait(0.4)
    local variable = math.random(1, 2)
    if variable == 1 then
        lighting.FogEnd = 12
    else
        game.Workspace.Gravity = 30
    end
    if game.Workspace.Gravity == 30 then do
            game.Workspace.World.BrickColor = BrickColor.new("Mid gray")
            game.Workspace.SA.Transparency = 0
            game.Workspace.SA.CanCollide = true
            game.Workspace.SpaceRock.Transparency = 0
            game.Workspace.SpaceRock.CanCollide = true
            game.Workspace.Z.Transparency = 0
            game.Workspace.Z.CanCollide = true
            game.Workspace.SRock.Transparency = 0
            game.Workspace.SRock.CanCollide = true
            game.Workspace.L.Transparency = 0
            game.Workspace.L.CanCollide = true
            game.Workspace.Y.Transparency = 0
            game.Workspace.Y.CanCollide = true
            lighting.TimeOfDay = 1

            if game.Workspace.Gravity == 30 then
                wait(12)
                button.Transparency = 0
                button.CanCollide = true
                game.Workspace.Stand.Transparency = 0
                game.Workspace.Stand.CanCollide = true
                game.Workspace.Gravity = 128
                lighting.TimeOfDay = 14
            end
        end
    end
    if lighting.FogEnd == 12 then do
            game.Workspace.FindMe.Transparency = 0
            game.Workspace.FindMe.CanCollide = true
            game.Workspace.FindMe.ClickDetector.MouseClick:Connect(function()
                lighting.FogEnd = 1000
                button.CanCollide = true
                button.Transparency = 0
                game.Workspace.Stand.Transparency = 0
                game.Workspace.Stand.CanCollide = true
                game.Workspace.FindMe.CanCollide = false
                game.Workspace.FindMe.Transparency = 1
            end)
        end
    end
end)

2 answers

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

I am sure why you are using if workspace.Gravity == 30 then do, instead you should not add do there. Also, you are checking your Gravity value twice in the same code which is pretty much useless. Well, you can try doing :

if game.Workspace.Gravity == 30 then 
            game.Workspace.World.BrickColor = BrickColor.new("Mid gray")
            game.Workspace.SA.Transparency = 0
            game.Workspace.SA.CanCollide = true
            game.Workspace.SpaceRock.Transparency = 0
            game.Workspace.SpaceRock.CanCollide = true
            game.Workspace.Z.Transparency = 0
            game.Workspace.Z.CanCollide = true
            game.Workspace.SRock.Transparency = 0
            game.Workspace.SRock.CanCollide = true
            game.Workspace.L.Transparency = 0
            game.Workspace.L.CanCollide = true
            game.Workspace.Y.Transparency = 0
            game.Workspace.Y.CanCollide = true
            lighting.TimeOfDay = 1

            wait(12)

            button.Transparency = 0
            button.CanCollide = true
            game.Workspace.Stand.Transparency = 0
            game.Workspace.Stand.CanCollide = true
            game.Workspace.Gravity = 128
            lighting.TimeOfDay = 14
        end
    end

-- Also, remove do from 'Line 47'.
if lighting.FogEnd == 12 then

Now, I am going to simplify your code, so it doesn't look messy. For this, we will be creating two custom functions for Gravity and Fog.

function Gravity()
            game.Workspace.World.BrickColor = BrickColor.new("Mid gray")
            game.Workspace.SA.Transparency = 0
            game.Workspace.SA.CanCollide = true
            game.Workspace.SpaceRock.Transparency = 0
            game.Workspace.SpaceRock.CanCollide = true
            game.Workspace.Z.Transparency = 0
            game.Workspace.Z.CanCollide = true
            game.Workspace.SRock.Transparency = 0
            game.Workspace.SRock.CanCollide = true
            game.Workspace.L.Transparency = 0
            game.Workspace.L.CanCollide = true
            game.Workspace.Y.Transparency = 0
            game.Workspace.Y.CanCollide = true
            lighting.TimeOfDay = 1

            wait(12)

            button.Transparency = 0
            button.CanCollide = true
            game.Workspace.Stand.Transparency = 0
            game.Workspace.Stand.CanCollide = true
            game.Workspace.Gravity = 128
            lighting.TimeOfDay = 14
end

local function InsideMouseClick()
    lighting.FogEnd = 1000
                button.CanCollide = true
                button.Transparency = 0
                game.Workspace.Stand.Transparency = 0
                game.Workspace.Stand.CanCollide = true
                game.Workspace.FindMe.CanCollide = false
                game.Workspace.FindMe.Transparency = 1
end

function Fog()
    if lighting.FogEnd == 12 then
            game.Workspace.FindMe.Transparency = 0
            game.Workspace.FindMe.CanCollide = true
            game.Workspace.FindMe.ClickDetector.MouseClick:Connect(InsideMouseClick)
        end
end

local variable = math.random(1, 2)
    if variable == 1 then
        Fog()
    else
        Gravity() -- It fires our function that we made above
    end

This is how your code might look:

--//Variables
local lighting = game.Lighting
local button = script.Parent
local ClickDetector = Instance.new("ClickDetector")
ClickDetector.Parent = button
ClickDetector.MaxActivationDistance = 15

--// Functions
function Gravity()
        workspace.Gravity = 30
            game.Workspace.World.BrickColor = BrickColor.new("Mid gray")
            game.Workspace.SA.Transparency = 0
            game.Workspace.SA.CanCollide = true
            game.Workspace.SpaceRock.Transparency = 0
            game.Workspace.SpaceRock.CanCollide = true
            game.Workspace.Z.Transparency = 0
            game.Workspace.Z.CanCollide = true
            game.Workspace.SRock.Transparency = 0
            game.Workspace.SRock.CanCollide = true
            game.Workspace.L.Transparency = 0
            game.Workspace.L.CanCollide = true
            game.Workspace.Y.Transparency = 0
            game.Workspace.Y.CanCollide = true
            lighting.TimeOfDay = 1

            wait(12)

            button.Transparency = 0
            button.CanCollide = true
            game.Workspace.Stand.Transparency = 0
            game.Workspace.Stand.CanCollide = true
            game.Workspace.Gravity = 128
            lighting.TimeOfDay = 14
end

local function InsideMouseClick()
    lighting.FogEnd = 1000
                button.CanCollide = true
                button.Transparency = 0
                game.Workspace.Stand.Transparency = 0
                game.Workspace.Stand.CanCollide = true
                game.Workspace.FindMe.CanCollide = false
                game.Workspace.FindMe.Transparency = 1
end

function Fog()
        lighting.FogEnd = 12 
            game.Workspace.FindMe.Transparency = 0
            game.Workspace.FindMe.CanCollide = true
            game.Workspace.FindMe.ClickDetector.MouseClick:Connect(InsideMouseClick)
end

--//Main
ClickDetector.MouseClick:Connect(function()
    print("Button has been clicked.")
    button.Transparency = 1
    game.Workspace.Stand.Transparency = 1
    game.Workspace.Stand.CanCollide = false
    button.CanCollide = false
    wait(0.4)
    local variable = math.random(1, 2)
    if variable == 1 then
        Fog()
    else
        Gravity()
    end
end)

Lemme know if it helps!

0
Thank you! You neated up my code and made everything better. I have added some of my own code to it and your way of presenting it and I was able to ressolve the issue. TgaTheGoldenArmour 15 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

So LUA can interpret the code as if Gravity is a member of Workspace and not a propriety.

Just say if the variable = 1, then you put all the fog code into it. And if the avriable is 2, you put all the Gravity code into it:

local lighting = game.Lighting
local button = script.Parent
local ClickDetector = Instance.new("ClickDetector")
ClickDetector.Parent = button
ClickDetector.MaxActivationDistance = 15

ClickDetector.MouseClick:Connect(function()
    print("Button has been clicked.")
    button.Transparency = 1
    game.Workspace.Stand.Transparency = 1
    game.Workspace.Stand.CanCollide = false
    button.CanCollide = false
    wait(0.4)
    local variable = math.random(1, 2)
    if variable == 1 then
        lighting.FogEnd = 12
    game.Workspace.FindMe.Transparency = 0
          game.Workspace.FindMe.CanCollide = true
          game.Workspace.FindMe.ClickDetector.MouseClick:Connect(function()
           lighting.FogEnd = 1000
           button.CanCollide = true
                button.Transparency = 0
                game.Workspace.Stand.Transparency = 0
                game.Workspace.Stand.CanCollide = true
                game.Workspace.FindMe.CanCollide = false
                game.Workspace.FindMe.Transparency = 1
    else
        game.Workspace.Gravity = 30
           game.Workspace.World.BrickColor = BrickColor.new("Mid gray")
            game.Workspace.SA.Transparency = 0
            game.Workspace.SA.CanCollide = true
            game.Workspace.SpaceRock.Transparency = 0
            game.Workspace.SpaceRock.CanCollide = true
            game.Workspace.Z.Transparency = 0
            game.Workspace.Z.CanCollide = true
            game.Workspace.SRock.Transparency = 0
            game.Workspace.SRock.CanCollide = true
            game.Workspace.L.Transparency = 0
            game.Workspace.L.CanCollide = true
            game.Workspace.Y.Transparency = 0
            game.Workspace.Y.CanCollide = true
            lighting.TimeOfDay = 1


              wait(12)
                button.Transparency = 0
                button.CanCollide = true
                game.Workspace.Stand.Transparency = 0
              game.Workspace.Stand.CanCollide = true
              game.Workspace.Gravity = 128
             lighting.TimeOfDay = 1
    end 
end)

It's probably a LUA glitch treating proprieties as instances, I've had this error before.

Answer this question