Hello,
I have an issue with a local script which make you able to place stuctures. When I click the gui button a clone of the structure gets created and you can easly move it around using the mouse. I can also rotate it just by clicking R. The structure exists only in the client until the player decides to place it (mouse click) and then thanks to an event the structure gets placed by a server script.
The Problem:
If the player decides to not place the structure he can click X to delete it and the structure (existing only in the client) gets destroyed. It seems working but if the player runs again the script by clicking the gui button and tries to rotate the item it will rotate 2 times instead of 1 as if there were two structures instead of one.
Idk what i’m doing wrong because the script runs multiple times even if the destroyed structure = nil
local RKeyPressed
local function Placing()
for i, Tables in pairs(TableList:GetChildren()) do
if Tables:IsA("Frame") then --Checking if the Button Clicked is from the Build UI
local structureButton = Tables.UniversalButton
if structureButton:IsA("ImageButton") then
structureButton.MouseButton1Up:Connect(function()
local goodToPlace = false
local placedStructure
local deletedStrucuture
if placingStructure == false and structureButton then
placingStructure = true
goodToPlace = true
local clientStructure = Table:FindFirstChild(structureButton.Parent.Name):Clone() --The Table which the player see and can move
clientStructure.Parent = game.Workspace.Tables
UIS.InputBegan:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.R then
RKeyPressed = true
local rotation = 90
if clientStructure then
if placingStructure == true then
yOrientation = yOrientation + rotation
-- now changing the CFrame (toggled)
UIS.InputEnded:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.R then
rKeyIsPressed = false
end
end)
UIS.InputBegan:Connect(function(Input)
if Input.UserInputType == Enum.UserInputType.MouseButton1 then
if placingStructure == true then
if goodToPlace == true then
-- Invoking a server event (toggled)
clientStructure:Destroy()
placingstrucutre = false
task.wait(0.2)
Function1IsRunning = false
UIS.InputBegan:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.X then
if placingStructure == true then
if clientStructure then
--Invoking Deleting Event (toggled) that returns deletedStrucuture = true
if deletedStrucuture == true then
clientStructure:Destroy()
end
placingStructure = false
Function1IsRunning = false
Setting whatever variable holds the structure Instance won’t destroy it, it’ll just rid it from memory in that script. You have to call :Destroy() on it. structure:Destroy().
The only way your script would be running twice, is if you disable it and then reenable it, or if you reparent it, or if you clone it. In which cloning is the same is reparenting. If you clone an item that has a script in it, it’ll will also run again. Make sure you’re not doing any of these things and you should be good.
local RotationConnection = UIS.InputBegan:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.R then
RKeyPressed = true
local rotation = 90
if clientStructure then
if placingStructure == true then
yOrientation = yOrientation + rotation
UIS.InputBegan:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.X then
if placingStructure == true then
if clientStructure then
--Invoking Deleting Event (toggled) that returns deletedStrucuture = true
if deletedStrucuture == true then
clientStructure:Destroy()
end
placingStructure = false
Function1IsRunning = false
RotationConnection:Disconnect()