Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How To Despawn A Model When A Player Leaves?

Asked by 3 years ago
Edited 3 years ago

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

Log in to vote
0
Answered by
Xapelize 2658 Moderation Voter Community Moderator
3 years ago

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

0
Also, you could enable the bot for spawning in the script later blah blah Xapelize 2658 — 3y
0
wth your game is growing sonic it's 70k+ visits :scream: :how: Xapelize 2658 — 3y
0
XAP COMING IN WITH THE CLUTCH AGAIN THANK YOU EliteNoscoper1337 7 — 3y
0
i never get more than 200 visit in my game lol how wth Xapelize 2658 — 3y
View all comments (2 more)
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 EliteNoscoper1337 7 — 3y
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 EliteNoscoper1337 7 — 3y
Ad

Answer this question