Hello, my building system is for some reason not working properly. When I place a block it doesnt identify the new location of the block. Could someone help?
-- // Copyright 2023, lambarini, All rights reserved.
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Events = ReplicatedStorage:WaitForChild("Events")
local Objects = ReplicatedStorage:WaitForChild("Objects")
local Blocks = Objects:WaitForChild("Blocks")
local RemoteEvent = Events:WaitForChild("RemoteEvent")
local CurrentBlockPos = nil
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local Camera = workspace.CurrentCamera
local function Location(Block)
local Target = Mouse.Target
local Surface = Mouse.TargetSurface
local Result = nil
if Target == Block then
return Block.Position
end
if Target and Surface then
if Surface == Enum.NormalId.Back then
Result = Target.Position + Vector3.new(0,0,3)
elseif Surface == Enum.NormalId.Front then
Result = Target.Position + Vector3.new(0,0,-3)
elseif Surface == Enum.NormalId.Top then
Result = Target.Position + Vector3.new(0,3,0)
elseif Surface == Enum.NormalId.Bottom then
Result = Target.Position + Vector3.new(0,-3,0)
elseif Surface == Enum.NormalId.Right then
Result = Target.Position + Vector3.new(3,0,0)
elseif Surface == Enum.NormalId.Left then
Result = Target.Position + Vector3.new(-3,0,0)
end
end
return Result
end
local Block = Blocks.Grass:Clone()
Block.Parent = workspace
Block.Transparency = 0.4
Block.CanCollide = false
RunService.RenderStepped:Connect(function()
local Pos = Location(Block)
if Pos then
CurrentBlockPos = Pos
Block.Position = CurrentBlockPos
Block.Color = Color3.fromRGB(0,255,0)
else
Block.Color = Color3.fromRGB(255,0,0)
end
end)
Mouse.Button1Down:Connect(function()
RemoteEvent:FireServer("Place","Grass",CurrentBlockPos)
Block:Destroy()
Block = Blocks.Grass:Clone()
Block.Parent = workspace
Block.Transparency = 0.4
Block.CanCollide = false
end)