I have made this script where it spawns the thing if it is either timeup or lost from game. However, in both timeup or lostgame state the item spamspawns and i cant figure out how to make it only spawn one time.. I have tried by making a boolean so the thing spawns when it is true, but it still doesn`t work. code:
local totemEye1 = script.Parent.eye1 local totoemEye2 = script.Parent.eye2 local players = game.Players local originalkeyTorch = game.ServerStorage:WaitForChild("keyTorch") local originalkeyPosition = originalkeyTorch.Handle.Position local keyTorchLostDetector = script.Parent.keyLostDetector local torchBackDetector = script.Parent.Parent.torch1Checker local keylost = false local keyreturned = false local keytorchTimer = game.StarterGui.ScreenGui.keytorchtimer local playerDefine = game.Players:FindFirstChildOfClass("Player") local ihavekey = false local count = 1 local foundtorchCount = 0 local sensorTouchCount = 1 local theivePrevious = false local respawnHappended = false local function keyReSpawn() if respawnHappended == false then wait(1) local visiblekeyTorch = originalkeyTorch:Clone() visiblekeyTorch.Name = "visiblekeyTorch" visiblekeyTorch.Parent = game.Workspace respawnHappended = true end end keyTorchLostDetector.Touched:Connect(function(hit) print("something touched ") if hit.Parent:FindFirstChild("Humanoid")then --the keystoler is humanoid print("the visible key torch is about to get stealed by player") if game.Workspace:FindFirstChild("visiblekeyTorch") == nil and ihavekey == false then ihavekey = true -- the key is held by humanoid keyreturned = false keylost = false print("the visiblekey is not in workspace") print("the key is owned by someone") end if ihavekey == true then--countdown till key is returned to totempole print("countdown starting") if count == 1 then game.ReplicatedStorage.torchcountdownStart:FireAllClients() count = count+10 end end wait(11)--waits 11 sec so if the torch was given back within 10sec, there is no delay. if keyreturned == true then keylost = false ihavekey = false theivePrevious = true else keylost = true foundtorchCount = 1 theivePrevious = false end if keylost == true and keyreturned == false then print("keyRespawnSequence") if foundtorchCount == 1 then keylost = false keyreturned = false wait(1) keyReSpawn() count = 1 foundtorchCount = 0 end end end end) torchBackDetector.Touched:Connect(function(hit) if hit:FindFirstAncestor("visiblekeyTorch") and foundtorchCount ==1 then print("the key was returned") theivePrevious = true foundtorchCount = 0 keylost = false keyreturned = true local destroyTarget = hit:FindFirstAncestor("visiblekeyTorch") destroyTarget:Destroy() end end)