Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
-1

Please help me fix this?

Asked by
mnaj22 44
5 years ago

It's not making the number of the GUi go up

local Player = script.Parent.Parent
local mouse = Player:GetMouse()

function Dig()
   if mouse.Target.Name == "Nope" then
    script.Parent.Disabled = true
end
    if mouse.Target ~= nil then
        if mouse.Target.Name == "Dirt" then
        mouse.Target.Transparency = mouse.Target.Transparency +1
        if mouse.Target.Transparency > 0.9 then
            mouse.Target:remove()
            script.Parent.Parent.PlayerGui.ScreenGui.Frame.Dirt.Value.Value = script.Parent.Parent.PlayerGui.ScreenGui.Frame.Dirt.Value.Value +1
        end
        end

        if mouse.Target.Name == "Silver" then
        mouse.Target.Transparency = mouse.Target.Transparency +0.5
        if mouse.Target.Transparency > 0.9 then
            mouse.Target:remove()
            script.Parent.Parent.PlayerGui.ScreenGui.Frame.Silver.Value.Value = script.Parent.Parent.PlayerGui.ScreenGui.Frame.Silver.Value.Value +1
        end
        end

        if mouse.Target.Name == "Diamond" then
        mouse.Target.Transparency = mouse.Target.Transparency +0.3
        if mouse.Target.Transparency > 0.9 then
            mouse.Target:remove()
            script.Parent.Parent.PlayerGui.ScreenGui.Frame.Diamond.Value.Value = script.Parent.Parent.PlayerGui.ScreenGui.Frame.Diamond.Value.Value +1
        end
        end

        if mouse.Target.Name == "Ruby" then
        mouse.Target.Transparency = mouse.Target.Transparency +0.2
        if mouse.Target.Transparency > 0.9 then
            mouse.Target:remove()
            script.Parent.Parent.PlayerGui.ScreenGui.Frame.Ruby.Value.Value = script.Parent.Parent.PlayerGui.ScreenGui.Frame.Ruby.Value.Value +1
        end
        end

        if mouse.Target.Name == "Emerald" then
        mouse.Target.Transparency = mouse.Target.Transparency +0.16
        if mouse.Target.Transparency > 0.9 then
            mouse.Target:remove()
            script.Parent.Parent.PlayerGui.ScreenGui.Frame.Emerald.Value.Value = script.Parent.Parent.PlayerGui.ScreenGui.Frame.Emerald.Value.Value +1
        end
        end

        if mouse.Target.Name == "Gold" then
        mouse.Target.Transparency = mouse.Target.Transparency +0.01
        if mouse.Target.Transparency > 0.9 then
            mouse.Target:remove()
            script.Parent.Parent.PlayerGui.ScreenGui.Frame.Gold.Value.Value = script.Parent.Parent.PlayerGui.ScreenGui.Frame.Gold.Value.Value +1
        end
        end

        end

end

mouse.Button1Down:connect(Dig)



0
The number won't go up even though the GUi is connect to the Value mnaj22 44 — 5y

1 answer

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

First: tab your code correctly.

Issue:

  • You check if mouse.Target is not nil after using it (line 5)

  • You use script.Parent.Parent over and over even though you have Player defined to be the same thing


After fixing those, have a massive amount of code reuse. For instance, just mouse.Target appears 32 times.

Simple fix: let hover = mouse.Target.

Let's get rid of the "Nope" case. It doesn't make sense to disable scripts -- that's dangerous. It also doesn't fit in with the rest of what's going on.

function Dig()
    local hover = mouse.Target
    if hover then -- Not nil
        if hover.Name == "Dirt" then
            hover.Transparency = hover.Transparency +1
            if hover.Transparency > 0.9 then
                hover:remove()
                Player.PlayerGui.ScreenGui.Frame.Dirt.Value.Value = Player.PlayerGui.ScreenGui.Frame.Dirt.Value.Value +1
            end
        end
    ....

Now we are repeatedly looking at hover.Name. Let's use kind and also use elseifs.

function Dig()
    local hover = mouse.Target
    if hover then -- Not nil
        local kind = hover.Name
        if kind == "Dirt" then
            hover.Transparency = hover.Transparency +1
            if hover.Transparency > 0.9 then
                hover:remove()
                Player.PlayerGui.ScreenGui.Frame.Dirt.Value.Value = Player.PlayerGui.ScreenGui.Frame.Dirt.Value.Value +1
            end
        elseif kind == "Silver" then
            hover.Transparency = hover.Transparency +0.5
            if hover.Transparency > 0.9 then
                hover:remove()
                Player.PlayerGui.ScreenGui.Frame.Silver.Value.Value = Player.PlayerGui.ScreenGui.Frame.Silver.Value.Value +1
            end
    ....

Now there is still a massive amount of reuse.

This chunk appears over and over:

hover.Transparency = hover.Transparency + AAA
if hover.Transparency > 0.9 then
    hover:Remove()
    Player.PlayerGui.ScreenGui.Frame.BBB.Value.Value = Player.Parent.PlayerGui.ScreenGui.Frame.BBB.Value.Value +1
end

The only difference is that AAA depends on the kind of material, and BBB is the name of hover.

Let's use that, and instead of repeating ourselves, use the same code to do this:

function Dig()
    local hover = mouse.Target
    if hover then -- Not nil
        local kind = hover.Name
        local rates = {
            Dirt = 1,
            Silver = 0.5,
            Diamond = 0.3,
            Ruby = 0.2,
            Emerald = 0.16,
            Gold = 0.01
        }
        local rate = rates[kind]
        if rate then
            -- It is a kind of block we can mine
            hover.Transparency = hover.Transparency + rate
            if hover.Transparency > 0.9 then
                hover:Remove()
                Player.PlayerGui.ScreenGui.Frame[kind].Value.Value = Player.PlayerGui.ScreenGui.Frame[kind].Value.Value + 1
            end
        end
    end
end

We should probably just have a reference to the inventory = Player.PlayerGui.ScreenGui.Frame to avoid that extra long line (and let us move it later, if need be)

local Player = script.Parent.Parent
local mouse = Player:GetMouse()
local inventory = Player.PlayerGui.ScreenGui.Frame
                ....
                inventory[kind].Value.Value = inventory[kind].Value.Value + 1
                ....

Now that we have reasonable code....

What does it do instead? Is there an error? Have you tried anything at all to figure out what's wrong yourself?

Are you sure there is a Value object called Value inside of, e.g., inventory.Dirt? Are you sure that Dirt isn't just the Vlaue object?

Ad

Answer this question