I am working on a placement system and i use task.spawn to show the preview and task.cancel to stop that, the issue is it doesn’t seem to work, with the spawned task still continueing after i did task.cancel.
Code:
local thread
local Model
local function Placement(Model)
RunService.RenderStepped:Connect(function()
print(Model)
end)
end
UserInputService.InputBegan:Connect(function(Input, IsChatting)
if IsChatting then return end
if script.Parent.Parent ~= Players.LocalPlayer.Character then return end
if Input.KeyCode == Enum.KeyCode.E then
if not Showing then
print("on")
Model = ReplicatedStorage.CookingRemotes[script.Parent.Name]:Clone()
Mouse.TargetFilter = Model
Model:PivotTo(CFrame.new() * CFrame.Angles(math.rad(0), math.rad(Players.LocalPlayer.Character.Torso.Rotation.Y), math.rad(0)))
Showing = true
Model.Parent = workspace
thread = task.spawn(Placement, Model)
else
print("off")
task.cancel(thread)
Showing = false
Model:Destroy()
end
end
end)
script.Parent.Unequipped:Connect(function()
if Showing then
task.cancel(thread)
Showing = false
Model:Destroy()
end
end)
Any help is appreciated!