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: