Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How To Despawn A Model When A Player Leaves?

Hello again, sorry for posting back here so soon, but I've ran into another issue.

Essentially, this is a part 2 to a previous post I made regarding the spawns in my game, of which you can find here.

Our game's biggest problem is people spawning in all the robots, and despawning other people's robots. With help from a lovely member on here, that problem has been solved. However we have since ran into a new problem that I thought would be easy to fix, but is far from it.

If a player spawns a bot and then leaves the server, the bot stays in and nobody can despawn it, this can be repeated multiple times and can cause immense lag and disruption in the server.

This is the current script for the spawners:

local Robot = game.ServerStorage:FindFirstChild("Aegis",true) 
local Spawned = script.Parent:FindFirstChild("Spawned",true)

function onClicked(player)
	
	BotSpawned = player.BoolValue
	BotBeingUsed = player.StringValue
	
	if Spawned.Value == false and BotSpawned.Value == false then
		BotBeingUsed.Value = 'Aegis'
		BotSpawned.Value = true
		backup = Robot:clone()	
		backup.Parent = workspace
		backup:makeJoints()
		Spawned.Value = true
		script.Parent.BrickColor = BrickColor.new("Really red")
	elseif Spawned.Value == true then
		if BotBeingUsed.Value == 'Aegis' then
			BotBeingUsed.Value = 'None'
			BotSpawned.Value = false
			backup:Destroy()
			Spawned.Value = false
			script.Parent.BrickColor = BrickColor.new("Lime green")
		end
	end
end


while Spawned.Value == true do
	for i, player in pairs(game.Players:GetPlayers()) do
		if player.StringValue.Value ~= 'Aegis' then
			backup:Destroy()
			Spawned.Value = false
			script.Parent.BrickColor = BrickColor.new("Lime green")
			break
		else
			wait()
		end
	end
end


script.Parent.ClickDetector.MouseClick:connect(onClicked)

The part from "While Spawned.Value == true do" and blow is my attempt to fix said issue, however it has yet to be successful, if someone could help me out to fix this one problem, the game's biggest issue can be solved.

I am quite new to this level of scripting, so excuse me if I don't quite comprehend things right/have bad code.

Thank you for reading.

1 answer

Another concept:

You should use a script, detects if a player leaves, locate his string value and destroy the string value's spaceship (sorry if i have bad explaining so heres an example)

game:GetService("Players").PlayerRemoving:Connect(function(player)
	local String = player.StringValue

	game:GetService("ServerStorage")[String.Value]:Destroy()
end

Simple as that! Well I hope it works and this is a concept so yeah (im not sure if it works)...... Put it in the serverscriptservice since it must be serversided

6 comments

0

Also, you could enable the bot for spawning in the script later blah blah

0

wth your game is growing sonic it's 70k+ visits :scream: :how:

0

XAP COMING IN WITH THE CLUTCH AGAIN THANK YOU

0

i never get more than 200 visit in my game lol how wth

0

we've had a small playerbase in our old game back when we were just some young kids fooling around doing whatever we could, and we were tapping into a player base that had yet to be fully taken yet

0

i think what got us such a big visit amount was the timing of the tv show, as when we did a BIG update that made the game more playable, it was when the tv show season ended and people wanted more robot destruction

This discussion is preserved as it was. New answers, comments, and votes are closed.