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

Cycling through buildings not working correctly?

Asked by 5 years ago

This script is supposed to work so if the player hits 'z' you enter build mode, then if you hit e you can cycle through buildings. Here's the script

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local placeablebuildings = {game.Workspace.TheFirstBuilding, game.Workspace.WallWood}
local cycle = 1

mouse.KeyDown:Connect(function(key)
    if key == ("z") then
        if player.Character.Building.Value == false then

            player.Character.Building.Value = true
            mouse.Move:Connect(function()
                if player.Character.Building.Value == false then
                    return nil
                end
                for c, d in pairs(game.Workspace.Building:GetChildren()) do
                    if d.Owner.Value == player.Name then
                        d:Destroy()
                    end
                end

                local building = placeablebuildings[cycle]:Clone() -- Error
                building.Parent = workspace.Building
                building.PrimaryPart = building.APrimaryPart
                building.Owner.Value = player.Name
                building:MoveTo(mouse.Hit.p)
                local placing = false

                mouse.KeyDown:Connect(function(otherkey)
                    if otherkey == ("e") then
                        cycle = cycle + 1
                        building:Destroy()
                        building = placeablebuildings[cycle]:Clone()
                        building.Parent = workspace.Building
                        building.PrimaryPart = building.APrimaryPart
                        building.Owner.Value = player.Name
                        building:MoveTo(mouse.Hit.p)
                    end
                end)
            end)
        end
    end
end)

Whenever I hit 'e', I get an error saying Players.DaBrainlessOne.PlayerScripts.BuildingScript:22: attempt to index field '?' (a nil value) This is a localscript in StarterPlayerScripts, and this script was shortened down so it doesn't take that long to read.

0
On line 4 when you define placeablebuildings, try removing the {}, if that doesn't work, put the {} back and on line 22, remove that whole line of code and retype it, because ? is usually used when the player has accidentally typed an invisible symbol. TheOnlySmarts 233 — 5y

Answer this question