Hello, I'm a developer for a Battlebots game, of which can be found here
One of the biggest issues that players have reported is that people can spawn several machines using our spawn buttons, and despawn other people's machines.
My goal is to figure out a way where people can only spawn a single machine, and can only despawn the machine they spawned. We do not use GUIs for the spawns, and our best scenario is to keep it that way.
Here is a script for one of the machine spawners,
Robot = game.ServerStorage:FindFirstChild("Aegis",true) Spawned = script.Parent:FindFirstChild("Spawned",true) function onClicked(hit) if Spawned.Value == false then backup = Robot:clone() backup.Parent = workspace backup:makeJoints() Spawned.Value = true script.Parent.BrickColor = BrickColor.new("Really red") elseif Spawned.Value == true then backup:Destroy() Spawned.Value = false script.Parent.BrickColor = BrickColor.new("Lime green") end end script.Parent.ClickDetector.MouseClick:connect(onClicked)
Each machine is a separate model put into server storage, where then it's loaded into the game and that's about it
There is most definitely a way to make this work, and I've tried a few ideas using bool values but have had no success
Where should I start with this concept?
My concept will be
Clone a StringValue and a BoolValue for each people once they start the game. The stringValue value is defaulted as None. BoolValue value is defaulted as false.
You should make an if statement before spawning the robot, if the boolvalue is false, make it true and set the StringValue value to the robot name, ELSE remove the robot that is named as the stringValue's value, and continue.
I hope your game growth! Good luck on your game!
This might solve your question Since your code uses global variables, change them to local variables
local Robot = game.ServerStorage:FindFirstChild("Aegis") -- Local variables are best to use since they can be called faster than global variables local Spawned = script.Parent:FindFirstChild("Spawned") function onClicked() -- Arguments in clickdetectors reference the player, since you never use the argument, you can leave it blank if not Spawned then -- This can be changed to not spawned (AKA just another writing for Spawned == false) local backup = Robot:Clone() backup.Parent = workspace backup:MakeJoints() Spawned.Value = true script.Parent.BrickColor = BrickColor.new("Really red") else -- You do not need to include Elseif unless you are wanting to check for something else other than Spawned == true backup:Destroy() Spawned.Value = false script.Parent.BrickColor = BrickColor.new("Lime green") end end script.Parent.ClickDetector.MouseClick:Connect(onClicked)
It's just rewriting the code to look better. It might work. Have you checked if the value actually changed? Does the spawned value have to be a BoolValue?