Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Ipairs isn't looping thru Buttons?

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?

3 comments

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.

0

Why are you using ipairs? Pairs should be your option, you aren't dealing with a numeric dictionary

0

Thanks - Got a new bug aswell .-.

1 answer

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.