Script that implemented this callback has been destroyed

Hey guys,

So I made a game, where players votes for a map. Then when the maps are spawned and the “MapScript” is invoked, then the player is given a certain gear to play with. But for some reason most of the time(not all the time) I get this error script that implemented this callback has been destroyed. Does anyone know how to fix it?

Could you show us your code? Since apparently the script is being destroyed ahead of time.

local Reward
local Length = script.Parent.MapSettings.Length
local IsGameInPlay = game.ReplicatedStorage.GameStats.IsGameInPlay
local minPlayers = game.ReplicatedStorage.GameStats.minPlayers.Value
local GameStatus = game.ReplicatedStorage.GameStats.GameStatus
script.Parent.StartSignal.OnInvoke = function(Status,availablePlrs) 
	if Status == "Start" then
		for c,player in pairs(availablePlrs) do
					for z=3,0,-1 do
						if z> 0 then
							GameStatus.Value = "Game Starts in "..z
						elseif z == 0 then
							GameStatus.Value = "GO!"
						end
					end
					local Psword = game.ServerStorage.Swords.Sword:Clone()
					Psword.Parent = player.Backpack
					player.Character.Humanoid.WalkSpeed = 64
					player.Character.Humanoid.JumpPower = 60		
				end
				
			for i=Length.Value,0,-1 do	
						--i = i-1
						print(i)
						
						for x, player in pairs(availablePlrs) do
					if player then
						
						local character = player.Character
						
						if not character then
							-- Left the game
							table.remove(availablePlrs,x) 
						else
							if character:FindFirstChild("GameTag") then
								-- The are still alive
								print(player.Name.." is still in the game!")
							else
								-- They are dead
								table.remove(availablePlrs,x)
								print(player.Name.." has been removed!")
							end
						end
					else
						table.remove(availablePlrs,x)
						print(player.Name.." has been removed!")
					end
				end
				print(#availablePlrs)
				GameStatus.Value =  i.." Seconds left"
				
				if #availablePlrs == 2 then
					-- Last person standing
					Reward = math.random(20,25)
					local playerCash = availablePlrs[1].Currency.Cash 
					local playerWins = availablePlrs[1].leaderstats.Wins
					GameStatus.Value = "The winner is "..availablePlrs[1].Name
					print(Reward)
					playerCash.Value = playerCash.Value + Reward
					playerWins.Value = playerWins.Value + 1
					availablePlrs[1]:LoadCharacter()
					break
					
				elseif #availablePlrs == 0 then
					GameStatus.Value =  "Nobody won!"
					for i,v in pairs(availablePlrs) do
					Reward = math.random(15,20)
					v.Cash.Value = v.Currency.Cash.Value + Reward
					end
					break
				elseif i == 0 then
					 GameStatus.Value =  "Times up!"
					for i,v in pairs(availablePlrs) do
					Reward = math.random(15,20)
					v.Currency.Cash.Value =  v.Currency.Cash.Value+ Reward
					end
					break
				end
				print(i)
				wait(1)
			end
			wait(5)
			game.Lighting:FindFirstChildOfClass("Sky"):Destroy()
				for i,v in pairs(availablePlrs) do
					v.Backpack:ClearAllChildren()
					v.Character.Humanoid.WalkSpeed = 16
					v.Character.Humanoid.JumpPower = 50
					v:LoadCharacter()
				end
				wait(5)
				GameStatus.Value = "Intermission"
				IsGameInPlay.Value = false
				if game.Players.NumPlayers >= minPlayers then
					print("Invoked Map Voting")
					workspace.MapVoting.Signals.MapVotingFunc:Invoke("Start")
				else
					GameStatus.Value =  "Waiting for players..."
				end		
	end
end```

This is the “MapScript”? If you have the problem in this script, at first glance, I would not realize why this happens.

Also remember that callbacks cannot be invoked directly.

Callbacks are write-only members of objects that are set to functions. You cannot invoke a callback directly — Roblox will internally trigger the callback function, and pass to it relevant information your callback handler/function will need.

You can find this complete article here.


Also remember to add this to these parts of the script.

local Psword = game.ServerStorage.Swords.Sword:Clone()
Psword.Parent = player.Backpack
Psword.Parent = player.StarterGear -- Or player:WaitForChild("StarterGear")
player.Character.Humanoid.WalkSpeed = 64
player.Character.Humanoid.JumpPower = 60
1 Like

So whats the fix though for the problem? Should I just use BindableEvent?

If you delete the script and then clone it again, or something similar, I recommend that you change that.

No I dont hav smth saying Script:Destroy() I only clone the map from server storage then I set the parent to a folder in workspace called “Map.” Then in VotingHandlerScript I do this workspace.Map:ClearAllChildren().

The workspace.Map:ClearAllChildren() causes the Script to get destroyed, which in the end causes the issue above. I’d suggest using a BindableEvent rather than a BindableFunction, since you are not returning anything.

Then how would I delete the map from workspace?