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

Ipairs isn't looping thru Buttons?

Asked by 5 years ago
local tycoons = game.Workspace.Tycoons:GetChildren()
local Mod = require(game.ServerScriptService.TycoonModule)
local Players = game:GetService("Players")


function ClaimPlot(hit,v)

        local player = Players:GetPlayerFromCharacter(hit.Parent)

        if hit.Parent:FindFirstChild("Humanoid") then
            if player:FindFirstChild("HasClaim").Value == false then
                Mod.SetOwner(hit.Parent, v.Owner)
                player:FindFirstChild("HasClaim").Value = true
                v:WaitForChild("ClaimPart"):Destroy()
            else
                print("e")

            end

        end
end -- End of function (ClaimPlot)


function BuyObject(player,v) 
  print(player.Name)

end


for i,v in ipairs(tycoons) do
    for i,s in ipairs(tycoons.Buttons) do   --Here is the problem

        s.Click.ClickDetector.MouseClick:Connect(function(player)
            BuyObject(player,v)
        end)
    end
end


for i,v in ipairs(tycoons) do
    v.ClaimPart.Touched:connect(function(hit)
         ClaimPlot(hit,v)
  end)
end


local function PlayerLeave(player)

for i,v in ipairs(game.Workspace.Tycoons:GetChildren()) do
        if player.Name == v:WaitForChild("Owner").Value then
            local Backup = game.ReplicatedStorage.BackupFolder.Tycoon
            local TycoonCFrame = v.Base.CFrame
            local NewTycoon = Backup:Clone()
            v:Destroy()
            NewTycoon.PrimaryPart = NewTycoon.Base 
            NewTycoon.Parent = game.Workspace.Tycoons
            NewTycoon:SetPrimaryPartCFrame(TycoonCFrame)
                   NewTycoon.ClaimPart.Touched:connect(function(hit)
                   ClaimPlot(hit,NewTycoon)
            end)
        end
    end    
end

game.Players.PlayerRemoving:Connect(PlayerLeave)

It won't loop thru the Buttons inside the line where it should have like -- Heres the problem Line 31 Can anyone help?

0
First of all, tycoons is an array, so tycoons.Buttons won't exist. Second, you can't loop through an object. You need to use :GetChildren() to retrieve an array you can loop through. User#26971 0 — 5y
0
Why are you using ipairs? Pairs should be your option, you aren't dealing with a numeric dictionary goodadmins 111 — 5y
0
Thanks - Got a new bug aswell .-. BosOfroblox 40 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
local tycoons = game.Workspace.Tycoons:GetChildren()
local Buttons = game.Workspace.Tycoons.Tycoon.Buttons:GetChildren()
local Mod = require(game.ServerScriptService.TycoonModule)
local Players = game:GetService("Players")


function ClaimPlot(hit,v)

        local player = Players:GetPlayerFromCharacter(hit.Parent)

        if hit.Parent:FindFirstChild("Humanoid") then
            if player:FindFirstChild("HasClaim").Value == false then
                Mod.SetOwner(hit.Parent, v.Owner)
                player:FindFirstChild("HasClaim").Value = true
                v:WaitForChild("ClaimPart"):Destroy()
            else
                print("e")

            end

        end
end -- End of function (ClaimPlot)


function BuyObject(player,v) 
  print(player.Name)

end


for i,v in pairs(Buttons) do

    v.Click.ClickDetector.MouseClick:connect(function(player)

    BuyObject(player,v)
    end)

end


for i,v in pairs(tycoons) do
    v.ClaimPart.Touched:connect(function(hit)
         ClaimPlot(hit,v)
  end)
end


local function PlayerLeave(player)

for i,v in pairs(game.Workspace.Tycoons:GetChildren()) do
        if player.Name == v:WaitForChild("Owner").Value then
            local Backup = game.ReplicatedStorage.BackupFolder.Tycoon
            local TycoonCFrame = v.Base.CFrame
            local NewTycoon = Backup:Clone()
            v:Destroy()
            NewTycoon.PrimaryPart = NewTycoon.Base 
            NewTycoon.Parent = game.Workspace.Tycoons
            NewTycoon:SetPrimaryPartCFrame(TycoonCFrame)
                   NewTycoon.ClaimPart.Touched:connect(function(hit)
                   ClaimPlot(hit,NewTycoon)
            end)
        end
    end    
end

game.Players.PlayerRemoving:Connect(PlayerLeave)

So Now I have another problem after getting that to work which is, whenever a player touches it aka claims, then leaves afterward. The new owner isn't able to click on it.

Ad

Answer this question