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

Player who owns tycoon touches other tycoon doors and no one can purchase them?

Asked by
Asalea 25
2 years ago

I have a tycoon game where the player touches a door, the money is deducted from their leaderboard stat, and they become the owner of that door. I have a check currently in place where the script checks if the user has a copperTycoon value in their player directory and won't let them buy another door so they don't buy every tycoon in the server.

However, when they go and touch another door that is not owned, another player who doesn't own any tycoons are now no longer able to purchase the door.

owner = script.Parent.Parent.Parent.ownerName
oreMachine = script.Parent.Parent.Parent.machineOn

factoryPrice = 250

local debounce = false

function onTouched(hit)

    if (debounce == false) then

        debounce = true

        local check = hit.Parent:FindFirstChild("Humanoid") --Find the player's humanoid that touched the part.

        if (check ~= nil) then --If the check finds a player humanoid, then...

            local player = game.Players:GetPlayerFromCharacter(hit.Parent) --Find the player who touched the part among everyone else in the game.

            if (player:FindFirstChild("leaderstats"):FindFirstChild("Cash") ~= nil) then

                if (player:FindFirstChild("copperFactory") ~= nil) then --Check if the player already owns this type of factory.

                    -- :)

                else

                    local player = game.Players:GetPlayerFromCharacter(hit.Parent) --Find the player who touched the part among everyone else in the game.

                    local cash = player.leaderstats.Cash.Value --Sets the player's Cash leaderboard stats to a local variable.

                    if cash >= factoryPrice then

                        player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - factoryPrice

                        owner.Value = hit.Parent.Name --Save player's playername in the OwnerName StringValue.

                        local ownedFactory = Instance.new("StringValue") --Add a StringValue to the player to show the game that they already own this type of factory.

                        ownedFactory.Name = "copperFactory"
                        ownedFactory.Parent = player

                        wait(0.65)

                        script.Parent:remove() --Delete the Door part.

                        oreMachine.Value = true --Turn on the machine.

                    end
                end
            end

            debounce = false

        end
    end
end

script.Parent.Touched:Connect(onTouched)

Thank you!

1 answer

Log in to vote
0
Answered by
Xapelize 2658 Moderation Voter Community Moderator
2 years ago
owner = script.Parent.Parent.Parent.ownerName
oreMachine = script.Parent.Parent.Parent.machineOn

factoryPrice = 250

local debounce = false

function onTouched(hit)

    if (debounce == false) then

        debounce = true

        local check = hit.Parent:FindFirstChild("Humanoid") --Find the player's humanoid that touched the part.

        if (check ~= nil) then --If the check finds a player humanoid, then...

            local player = game.Players:GetPlayerFromCharacter(hit.Parent) --Find the player who touched the part among everyone else in the game.

            if (player:FindFirstChild("leaderstats"):FindFirstChild("Cash") ~= nil) then

                if (player:FindFirstChild("copperFactory") ~= nil) then --Check if the player already owns this type of factory.

                    -- :)

                else

                    local player = game.Players:GetPlayerFromCharacter(hit.Parent) --Find the player who touched the part among everyone else in the game.

                    local cash = player.leaderstats.Cash.Value --Sets the player's Cash leaderboard stats to a local variable.

                    if cash >= factoryPrice then

                        player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - factoryPrice

                        owner.Value = hit.Parent.Name --Save player's playername in the OwnerName StringValue.

                        local ownedFactory = Instance.new("StringValue") --Add a StringValue to the player to show the game that they already own this type of factory.

                        ownedFactory.Name = "copperFactory"
                        ownedFactory.Parent = player

                        wait(0.65)

                        script.Parent:remove() --Delete the Door part.

                        oreMachine.Value = true --Turn on the machine.

                    end
                end
            end
        end

        debounce = false -- The change I've made
    end
end

script.Parent.Touched:Connect(onTouched)

I moved the line debounce = false below the check ~= nil statement

Before you need to pass the check ~= nil statement to turn the debounce back to false, and if it doesn't pass the check ~= nil, the debounce is true forever since it never gets turned to false

1
That fixed the issue completely! I must have missed it or got confused. Thank you! Asalea 25 — 2y
0
@T3_MasterGamer you are a cheetah imKirda 4491 — 2y
0
@ImKirda nob Xapelize 2658 — 2y
Ad

Answer this question