Place Structure staying in workspace after death

Basically I have a crafting gui which allows players to craft things such as canoes for example. It works fine except for the fact that if the player resets before placing the structure, the structure will stay in the workspace.

I tried to implement the Humanoid.Died function but it doesn’t seem to be working.

The code I tried to use:

local Players = game:GetService("Players")
								local client = Players.LocalPlayer

								local character
								client.CharacterAdded:Connect(function(_character)
									character = _character
									character.Humanoid.Died:Connect(function()
										placingStructure = false
										ClientStructure:Destroy()

										if buttonClickConnection ~= nil then buttonClickConnection:Disconnect()

										end
									end)
								end)

Full code(localscript)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Events = ReplicatedStorage:WaitForChild("Events")

local PlaceStructure = Events.PlaceStructure
local Structures = ReplicatedStorage:WaitForChild("Structures")

local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local player = game.Players.LocalPlayer
local StructureFrame = script.Parent.Parent
local char = player.Character or player.Character.Wait()
local HumanoidRootPart = char:WaitForChild("HumanoidRootPart")

local CraftButton = script.Parent.Craft


local mouse = player:GetMouse()

local yBuildingOffset = 4
local maxPlacingDistance = 30
local zKeyIsPressed = false
local placingStructure = false

local PlacementControls = player:WaitForChild("PlayerGui").Controls.PlacementControls






local buttonClickConnection

local CanBuild = script.Parent.hasMaterials

CraftButton.MouseButton1Up:Connect(function()
	if CanBuild.Value == true then


		StructureFrame.Visible = false

		local yOrientation = 0
		local goodToPlace = false
		local PlacedStructure

		if placingStructure == false then
			placingStructure = true
			
			PlacementControls.Visible = true

			local ClientStructure = Structures:FindFirstChild(script.Parent.Name):Clone()
			ClientStructure.BrickColor = BrickColor.new("Forest green")
			ClientStructure.Material = "Neon"
			ClientStructure.CanCollide = false
			ClientStructure.Parent = game.Workspace
			
		

			local StartingCFrame = CFrame.new(0, -2, -15)
			ClientStructure.CFrame = HumanoidRootPart.CFrame:ToWorldSpace(StartingCFrame)

			RunService.RenderStepped:Connect(function()
				local MouseRay = mouse.UnitRay
				local castRay = Ray.new(MouseRay.Origin, MouseRay.Direction * 1000)
				local IgnoreList = {ClientStructure, char}
				local hit, position = workspace:FindPartOnRayWithIgnoreList(castRay, IgnoreList)

				if hit and (hit:IsA("Model") or  hit.Name:lower() == "water") and (HumanoidRootPart.Position - ClientStructure.Position).Magnitude < maxPlacingDistance then
					goodToPlace = true
					ClientStructure.BrickColor = BrickColor.new("Forest green")
				else
					goodToPlace = false
					ClientStructure.BrickColor = BrickColor.new("Crimson")
				end

				local newAnglesCFrame = CFrame.Angles(0, math.rad(yOrientation), 0)
				local newCFrame = CFrame.new(position.X, position.Y + yBuildingOffset, position.Z)
				ClientStructure.CFrame = newCFrame * newAnglesCFrame
			end)
			
			UIS.InputBegan:Connect(function(input)
				if input.KeyCode == Enum.KeyCode.X then
					PlacementControls.Visible = false
					placingStructure = false
					ClientStructure:Destroy()
					StructureFrame.Visible = true
					PlacementControls.Visible = false
					if buttonClickConnection ~= nil then buttonClickConnection:Disconnect()

					end

				end

			end)

			UIS.InputBegan:Connect(function(input)
				if input.KeyCode == Enum.KeyCode.Z then
					zKeyIsPressed = true

					local RotationSpeed = 5
					while zKeyIsPressed do
						wait()
						if placingStructure == true then
							yOrientation = yOrientation + RotationSpeed
						end
					end
				end
			end)

			UIS.InputEnded:Connect(function(input)
				if input.KeyCode == Enum.KeyCode.Z then
					zKeyIsPressed = false
				end
			end)

			buttonClickConnection = UIS.InputBegan:Connect(function(input)
				if input.UserInputType == Enum.UserInputType.MouseButton1 then
					if placingStructure == true then
						if goodToPlace == true then
							local structureCFrame = ClientStructure.CFrame + Vector3.new(0,5,0)
							PlacedStructure = PlaceStructure:InvokeServer("Canoe",structureCFrame)

							if PlacedStructure == true then
								placingStructure = false
								ClientStructure:Destroy()
								StructureFrame.Visible = true
								PlacementControls.Visible = false
								if buttonClickConnection ~= nil then buttonClickConnection:Disconnect()

								end
								
							
								local Players = game:GetService("Players")
								local client = Players.LocalPlayer

								local character
								client.CharacterAdded:Connect(function(_character)
									character = _character
									character.Humanoid.Died:Connect(function()
										placingStructure = false
										ClientStructure:Destroy()

										if buttonClickConnection ~= nil then buttonClickConnection:Disconnect()

										end
									end)
								end)



							end
						end
					end
				end
			end)
		end
	elseif CanBuild.Value == false then
		print("Dont have enough materials")
	end
	
	
end)



Heres an example:

resetting:
https://gyazo.com/47a0d1ff7e27afc10ab3f5574630b7b4

aftermath:

1 Like

Why not just make a seperate variable for the humanoid altogether?

	local character
	client.CharacterAdded:Connect(function(_character)
		    character = _character
            local humanoid = character:WaitForChild("Humanoid")
			humanoid.Died:Connect(function()
			    placingStructure = false
			    ClientStructure:Destroy()
                 
                if buttonClickConnect ~= nil then buttonClickConnection:Disconnect()
end)

If that doesn’t work why not check if the humanoid’s health <= 0?

1 Like

I attempted to do that previously but it didn’t work. I just tried your method and the problem is still occuring so ill try and do the :getpropertychangedsignal on the players health and hopefully that will work.

Alright

After you try that solution, try making an if statement where it checks if the humanoids health is less than or equal to 0

1 Like

The issue was that the function was inside of a mouseclick function. I placed the code below, above the buttonclick function and it now works. Thanks for the help

local char = player.Character or player.CharacterAdded:Wait()
			local playerHumanoid = char:WaitForChild("Humanoid")		

			playerHumanoid.HealthChanged:Connect(function(Health)
				
				if Health <= 0  then
				ClientStructure:Destroy()
					placingStructure = false
					end

				end)

Working code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Events = ReplicatedStorage:WaitForChild("Events")

local PlaceStructure = Events.PlaceStructure
local Structures = ReplicatedStorage:WaitForChild("Structures")

local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local player = game.Players.LocalPlayer
local StructureFrame = script.Parent.Parent
local char = player.Character or player.Character.Wait()
local HumanoidRootPart = char:WaitForChild("HumanoidRootPart")

local CraftButton = script.Parent.Craft


local mouse = player:GetMouse()

local yBuildingOffset = 4
local maxPlacingDistance = 30
local zKeyIsPressed = false
local placingStructure = false

local PlacementControls = player:WaitForChild("PlayerGui").Controls.PlacementControls






local buttonClickConnection

local CanBuild = script.Parent.hasMaterials

CraftButton.MouseButton1Up:Connect(function()
	if CanBuild.Value == true then


		StructureFrame.Visible = false

		local yOrientation = 0
		local goodToPlace = false
		local PlacedStructure

		if placingStructure == false then
			placingStructure = true
			
			PlacementControls.Visible = true

			local ClientStructure = Structures:FindFirstChild(script.Parent.Name):Clone()
			ClientStructure.BrickColor = BrickColor.new("Forest green")
			ClientStructure.Material = "Neon"
			ClientStructure.CanCollide = false
			ClientStructure.Parent = game.Workspace
			
		

			local StartingCFrame = CFrame.new(0, -2, -15)
			ClientStructure.CFrame = HumanoidRootPart.CFrame:ToWorldSpace(StartingCFrame)

			RunService.RenderStepped:Connect(function()
				local MouseRay = mouse.UnitRay
				local castRay = Ray.new(MouseRay.Origin, MouseRay.Direction * 1000)
				local IgnoreList = {ClientStructure, char}
				local hit, position = workspace:FindPartOnRayWithIgnoreList(castRay, IgnoreList)

				if hit and (hit:IsA("Model") or  hit.Name:lower() == "water") and (HumanoidRootPart.Position - ClientStructure.Position).Magnitude < maxPlacingDistance then
					goodToPlace = true
					ClientStructure.BrickColor = BrickColor.new("Forest green")
				else
					goodToPlace = false
					ClientStructure.BrickColor = BrickColor.new("Crimson")
				end

				local newAnglesCFrame = CFrame.Angles(0, math.rad(yOrientation), 0)
				local newCFrame = CFrame.new(position.X, position.Y + yBuildingOffset, position.Z)
				ClientStructure.CFrame = newCFrame * newAnglesCFrame
			end)
			
			UIS.InputBegan:Connect(function(input)
				if input.KeyCode == Enum.KeyCode.X then
					PlacementControls.Visible = false
					placingStructure = false
					ClientStructure:Destroy()
					StructureFrame.Visible = true
					PlacementControls.Visible = false
					if buttonClickConnection ~= nil then buttonClickConnection:Disconnect()

					end

				end

			end)

			UIS.InputBegan:Connect(function(input)
				if input.KeyCode == Enum.KeyCode.Z then
					zKeyIsPressed = true

					local RotationSpeed = 5
					while zKeyIsPressed do
						wait()
						if placingStructure == true then
							yOrientation = yOrientation + RotationSpeed
						end
					end
				end
			end)

			UIS.InputEnded:Connect(function(input)
				if input.KeyCode == Enum.KeyCode.Z then
					zKeyIsPressed = false
				end
			end)
			
			local char = player.Character or player.CharacterAdded:Wait()
			local playerHumanoid = char:WaitForChild("Humanoid")		

			playerHumanoid.HealthChanged:Connect(function(Health)
				
				if Health <= 0  then
				ClientStructure:Destroy()
					placingStructure = false
					end
				end)
			

			buttonClickConnection = UIS.InputBegan:Connect(function(input)
				if input.UserInputType == Enum.UserInputType.MouseButton1 then
					if placingStructure == true then
						if goodToPlace == true then
							local structureCFrame = ClientStructure.CFrame + Vector3.new(0,5,0)
							PlacedStructure = PlaceStructure:InvokeServer("Canoe",structureCFrame)

							if PlacedStructure == true then
								placingStructure = false
								ClientStructure:Destroy()
								StructureFrame.Visible = true
								PlacementControls.Visible = false
								if buttonClickConnection ~= nil then buttonClickConnection:Disconnect()
									

								end

							end
							
							
						end
					end
				end
			end)
		end
	elseif CanBuild.Value == false then
		print("Dont have enough materials")
	end
	
	
end)
1 Like

Actually I was wondering if that was it because I saw an extra end lol
Glad that its working now! :grin:

1 Like