Help with a vehicle spawner

Sorry for the long topic.

Hi There! I’m creating a obby called Hell Obby 2, a obby consisting of 100 unique levels, breaking the convetional rules of an obby.

Anyway, i’m creating stage 15, in which the player must ride a motorcycle, and try to survive to the end.

I’ve done almost all of the scripting, i only have a problem, with the spawning of the motorcycle.
Basically, right now, when a player respawns the motorcycle (Cloning the sample in the ServerStorage) Any Clone that exists will be destroyed, just a little problem: The game is a multiplayer game, and i need to create various motorcycles for it to be beaten without long wait times or spammers.

Not Destroying any clones is not an option, due to the fact that a player can spawn 2 motorcycles in the same spot, seriously bugging the game, also, he can create multiple motorcycles, to slowndown the game.

Here is the script that i’m using right now:

location = game.Workspace
regen = game.ServerStorage.Vehicles.Motorcycle
save = regen:clone()
back = nil
function onClicked()
if back ~= nil then
back:remove()
end
back = save:clone()
back.Parent = location
back:MakeJoints()
end

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

So, basically, i want it so the the script i showed to do the following:

  • Destroy any motorcycles close to the new motorcycle.
  • Not allow motorcycle spawn if number of motorcycles is equal to the number of players.
  • And all of the functions that it already has.

Thanks for reading, if you have a solution, please answer to this Topic.

Once again, sorry for the long topic.

4 Likes

If you want to keep track of which player’s motorcycle is which, I’d put it in a dictionary with the player’s name as the key rather than as a global variable. Example:

back = {}
function onClicked(player)
   if back[player.Name] ~= nil then
      back[player.Name]:Destroy() -- just a small thing, remove is deprecated
   end
   back[player.Name] = save:Clone()
   back[player.Name].Parent = location
   back[player.Name]:MakeJoints()
end
2 Likes

Well thanks, just one more thing to solve, avoiding motorcycle to motorcycle collision, and while i’m at it, what about removing player to player collision. I heard about this feature called “Collision Groups” But i can’t understand how to use it, can you help me?

3 Likes

The documentation for it is here, I’ll look to see if I can find any good tutorials but you’d get better responses if you made a new thread.

Edit: For removing player to player collision, there’s a pretty good wiki tutorial which should be a good example for how you’d do the motorcycle collisions.

1 Like

May I suggest when you implement what @posatta said , make sure your code is less messy. In your post your code isn’t indented and before your variables put local as well. This will make your code easier to read and easier to find where your code is going wrong.

Well, i already implemented that.

Thanks, marked your answer as the solution.

Hey, just a question, what is the difference between:

local Variable = true

and

Variable = true
1 Like

The difference is mainly scope, if you define a variable without local then it’s a global variable and can be used anywhere. A local variable means it can just be used inside its scope. For example:

local x = 7
local y = 7
do -- other things with their own scope are if statements, loops, and functions. this is short which is why it's the example.
   x = 3
   local y = 3
end
print(x) -- prints 3
print(y) -- prints 7
2 Likes

Here is the section from the wiki that will answer your question:

A local variable is a variable that can only be accessed by other things in the same scope. A variable is made local by adding the keyword local before the variable. It is true that local variables are obtained faster than normal variables. This is because they are integrated into the current environment they were created in.

2 Likes

Ok, i get it, i also see local being applied to other elements like:

 local function OnTest()
 end

Is local in these cases also for the same purpose?

1 Like

Yeah, and they’re slightly faster like @waterrunner said.

Well then, i figured everything out, just gotta solve collision groups…

2 Likes