Created
April 14, 2023 14:14
-
-
Save RobertCodez/1022c9ba8efde85f0a71231549b991c2 to your computer and use it in GitHub Desktop.
Grid Based placement system
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local SoundService = game:GetService("SoundService") | |
local function destroyv(player,v) | |
if v:FindFirstChild("SelectionBox") then | |
v:FindFirstChildOfClass("SelectionBox"):Destroy() | |
end | |
if v:FindFirstChild(tostring(player.TempVal.SelectedObject.Value)) then | |
v:FindFirstChild(tostring(player.TempVal.SelectedObject.Value)):Destroy() | |
end | |
end | |
local function destroyv2(player,v) | |
if v:FindFirstChild("SelectionBox") then | |
v:FindFirstChildOfClass("SelectionBox"):Destroy() | |
end | |
if v:FindFirstChild(tostring(player.TempVal.SelectedUtil.Value)) then | |
v:FindFirstChild(tostring(player.TempVal.SelectedUtil.Value)):Destroy() | |
end | |
end | |
game.ReplicatedStorage.Events.SetSelectedObject.OnServerEvent:Connect(function(player, BuildingNumber) | |
for _,v in pairs(game.Workspace.Grid:GetChildren()) do | |
if v.ClassName == "Script" then continue end | |
destroyv(player,v) | |
end | |
player.TempVal:FindFirstChild("SelectedObject").Value = tonumber(BuildingNumber) | |
end) | |
game.ReplicatedStorage.Events.SetSelectedMaterial.OnServerEvent:Connect(function(player,Number) | |
print(Number) | |
player.TempVal.SelectedMaterial.Value = Number | |
end) | |
game.ReplicatedStorage.Events.SetSelectedUtility.OnServerEvent:Connect(function(player,Number) | |
for _,v in pairs(game.Workspace.Grid:GetChildren()) do | |
if v.ClassName == "Script" then continue end | |
destroyv2(player,v) | |
end | |
player.TempVal.SelectedUtil.Value = Number | |
end) | |
local function Main() | |
for _,v in pairs(game.Workspace.Grid:GetChildren()) do | |
if v.ClassName == "Script" then continue end | |
v.ClickDetector.MouseHoverEnter:Connect(function(player) | |
if player.TempVal.SelectedObject.Value ~= 0 then | |
local sb = game.ReplicatedStorage.SelectionBox:Clone() | |
sb.Parent = v | |
sb.Adornee = v | |
local Building = game.ReplicatedStorage.Buildings:FindFirstChild(tostring(player.TempVal.SelectedObject.Value)):Clone() | |
for i,v in pairs(Building:GetDescendants()) do | |
if v:IsA("BasePart") then | |
v.Transparency = 0.7 | |
v.CanCollide = false | |
end | |
end | |
Building.Parent = v | |
local cf = CFrame.new(v.Position + Vector3.new(0,Building.Stud.Value,0)) * CFrame.Angles(0,math.rad(tonumber(v:GetAttribute("RotationHouse"))),0) | |
Building:PivotTo(cf) | |
end | |
end) | |
v.ClickDetector.MouseHoverLeave:Connect(function(player) | |
if player.TempVal.SelectedObject.Value ~= 0 then | |
destroyv(player,v) | |
end | |
end) | |
v.ClickDetector.MouseClick:Connect(function(player) | |
if player.TempVal.SelectedObject.Value ~= 0 then | |
destroyv(player,v) | |
local Building = game.ReplicatedStorage.Buildings[tostring(player.TempVal.SelectedObject.Value)]:Clone() | |
local BuildingPrice = Building:FindFirstChild("Price").Value | |
if player:WaitForChild("stat").Cash.Value >= BuildingPrice then | |
if v:GetAttribute("Occupied") == "YES" then game.SoundService.SoundEffects.ClickFailed:Play() return end | |
if v:GetAttribute("Terrain") == "1" then game.SoundService.SoundEffects.ClickFailed:Play() return end | |
v:SetAttribute("Occupied","YES") | |
player.stat.Cash.Value -= BuildingPrice | |
v:SetAttribute("BuildingName",tostring(player.TempVal.SelectedObject.Value)) | |
v:SetAttribute("Upgrades","1|1") | |
game.ReplicatedStorage.Events.DataLoaded:FireClient(player) | |
game.ReplicatedStorage.Events.ShakeCamera:FireClient(player) | |
SoundService.SoundEffects.PlacedHouse:Play() | |
end | |
end | |
end) | |
end | |
end | |
-- Material | |
local function MainMaterial() | |
for _,v in pairs(game.Workspace.Grid:GetChildren()) do | |
if v.ClassName == "Script" then continue end | |
v.ClickDetector.MouseHoverEnter:Connect(function(player) | |
if player.TempVal.SelectedMaterial.Value ~= 0 then | |
if v:FindFirstChild("SelectionBox") then | |
v:FindFirstChild("SelectionBox"):Destroy() | |
end | |
local SelectionBox = game.ReplicatedStorage.SelectionBox:Clone() | |
SelectionBox.SurfaceTransparency = 0 | |
SelectionBox.SurfaceColor3 = BrickColor.new(game.ReplicatedStorage.Materials:FindFirstChild(player.TempVal.SelectedMaterial.Value).Color.Value).Color | |
SelectionBox.Parent = v | |
SelectionBox.Adornee = v | |
end | |
end) | |
v.ClickDetector.MouseHoverLeave:Connect(function(player) | |
if v:FindFirstChild("SelectionBox") then | |
v:FindFirstChild("SelectionBox"):Destroy() | |
end | |
end) | |
v.ClickDetector.MouseClick:Connect(function(player) | |
if player.TempVal.SelectedMaterial.Value == 0 then return end | |
local price = game.ReplicatedStorage.Materials:FindFirstChild(player.TempVal.SelectedMaterial.Value).Price.Value | |
if player.stat.Cash.Value >= price then | |
player.stat.Cash.Value -= price | |
v.Material = Enum.Material[game.ReplicatedStorage.Materials:FindFirstChild(player.TempVal.SelectedMaterial.Value).Material.Value] | |
v.BrickColor = BrickColor.new(game.ReplicatedStorage.Materials:FindFirstChild(player.TempVal.SelectedMaterial.Value).Color.Value) | |
v:SetAttribute("Terrain",game.ReplicatedStorage.Materials:FindFirstChild(player.TempVal.SelectedMaterial.Value).Name) | |
end | |
end) | |
end | |
end | |
Main() | |
MainMaterial() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment