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.
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