Vermilius
|
  |
| Joined: 15 Jan 2009 |
| Total Posts: 50 |
|
|
| 07 Apr 2016 07:14 PM |
Hello, I'd much appreciate if an experienced scripter would correct this issue for me.
I am making a battle ground place with 4 different maps. I want to be able to switch between maps using click buttons. To do this I have 4 separate bricks with Vars 1 through 4 and click detectors in them. Each map, maps1 through maps4, is stored in serverstorage. This script is supposed to copy the map that corresponds to the pressed button into the workspace (from serverstorage), and remove any existing map from the workspace.
I want a copy of maps1 to already be in the workspace when a server is created (maps1 is the default map). The problem with this script is that if I have maps1 through maps4 in the serverstorage AND a copy of maps1 already in the workspace, when I press a button the script copies 2 of the selected maps into the workspace (creates potential for lag). So say I press the button for maps2 (while an existing copy of maps1 is in the workspace) - It puts 2 of the maps2 in the workspace (it does however, remove the maps1 as intended). Maybe the script thinks that I had 2 of maps1's (1 in workspace and 1 in serverstorage), so it puts 2 copies of maps2 in the workspace when I press button 2?
Another issue is that this script is only written for 3 maps - I tried modifying it for 4 but the 4th switch does not work
Script:
Maps = {game.ServerStorage.maps1, game.ServerStorage.maps2, game.ServerStorage.maps3} open = false for i,v in pairs(script.Parent:GetChildren()) do v:WaitForChild("ClickDetector").MouseClick:connect (function() if open == false then open = true newMap = Maps[v.Var.Value]:Clone() newMap.Parent = workspace else open = false for _, Model in pairs(game.Workspace:GetChildren()) do if Model.Name == "maps1" or Model.Name == "maps2" or Model.Name == "maps3" then Model:remove() open = true newMap = Maps[v.Var.Value]:Clone() newMap.Parent = workspace end end end end) end
Please help! |
|
|
| Report Abuse |
|
|
cgjnm
|
  |
| Joined: 22 Dec 2011 |
| Total Posts: 2347 |
|
|
| 07 Apr 2016 07:17 PM |
--Clearing the map-- for i,v in pairs(workspace:GetChildren())do if v:lower():find('maps') then v:Destroy() end end
--Adding Map-- local Map = game.ServerStorage:FindFirstChild("Maps1") if Map then Map = Map:Clone() Map.Parent = workspace end |
|
|
| Report Abuse |
|
|
Vermilius
|
  |
| Joined: 15 Jan 2009 |
| Total Posts: 50 |
|
|
| 07 Apr 2016 07:18 PM |
| Note that this script is grouped into a model with the 4 buttons |
|
|
| Report Abuse |
|
|
Vermilius
|
  |
| Joined: 15 Jan 2009 |
| Total Posts: 50 |
|
|
| 07 Apr 2016 07:21 PM |
cgjnm,
thank you for replying -
So do those two scripts replace the one I already have, or where do I add them? |
|
|
| Report Abuse |
|
|
cgjnm
|
  |
| Joined: 22 Dec 2011 |
| Total Posts: 2347 |
|
|
| 07 Apr 2016 07:24 PM |
My first script will remove any map in the game, so place that directly after you first press the button.
The other section that adds the map, you will have to change "Maps1" to match the name you need, and that will go after the removal of the maps. |
|
|
| Report Abuse |
|
|
cgjnm
|
  |
| Joined: 22 Dec 2011 |
| Total Posts: 2347 |
|
|
| 07 Apr 2016 07:25 PM |
Since the script is in a model with the 4 buttons, it'd be easier if you named each button the same name as the map, and then use:
local Map = game.ServerStorage:FindFirstChild(Button.Name) if Map then Map = Map:Clone() end |
|
|
| Report Abuse |
|
|
Vermilius
|
  |
| Joined: 15 Jan 2009 |
| Total Posts: 50 |
|
|
| 07 Apr 2016 07:32 PM |
| So will this still be one script if I have to change "Maps1" to whatever map I need? How does that work with 4 different maps? Also then what would the final script look like? Sorry if I'm not understanding, I hardly understand scripting, I would need extremely specific explanations. |
|
|
| Report Abuse |
|
|
cgjnm
|
  |
| Joined: 22 Dec 2011 |
| Total Posts: 2347 |
|
|
| 07 Apr 2016 08:00 PM |
Alright, so in the model, have 4 parts, with a click detector, each part shall be named either: maps1, maps2, maps3 or maps4 (because that is the name of the maps in ServerStorage)
script inside of model:
for i,v in pairs(script.Parent:GetChildren())do if v:FindFirstChild("ClickDetector")then v.ClickDetector.MouseClick:connect(function() for _,b in pairs(workspace:GetChildren())do if b.Name:lower()==v.Name:lower() then b:Destroy() end end local Map = game.ServerStorage:FindFirstChild(v.Name) if Map then Map = Map:Clone() Map.Parent = workspace end end) end end
Hopefully works, post any errors if there is any |
|
|
| Report Abuse |
|
|
Vermilius
|
  |
| Joined: 15 Jan 2009 |
| Total Posts: 50 |
|
|
| 08 Apr 2016 05:09 PM |
| It spawns the maps properly but does not remove any existing maps. Please help with that. |
|
|
| Report Abuse |
|
|
cgjnm
|
  |
| Joined: 22 Dec 2011 |
| Total Posts: 2347 |
|
| |
|
Vermilius
|
  |
| Joined: 15 Jan 2009 |
| Total Posts: 50 |
|
|
| 09 Apr 2016 10:39 AM |
| The code doesn't seem to have any errors, it just doesn't remove maps like it is supposed to after you press a button. |
|
|
| Report Abuse |
|
|
cgjnm
|
  |
| Joined: 22 Dec 2011 |
| Total Posts: 2347 |
|
|
| 09 Apr 2016 05:06 PM |
| My code goes on the assumption that the buttons are named the same as the maps, is this so? |
|
|
| Report Abuse |
|
|
Vermilius
|
  |
| Joined: 15 Jan 2009 |
| Total Posts: 50 |
|
|
| 09 Apr 2016 05:20 PM |
Yes, I did everything as you stated. The code spawns the maps properly but does not remove the existing map first. Roblox does not show any errors in the code, but somehow the first half does nothing. Not sure why this is.
Do you think you can change it somehow? |
|
|
| Report Abuse |
|
|
cgjnm
|
  |
| Joined: 22 Dec 2011 |
| Total Posts: 2347 |
|
|
| 09 Apr 2016 10:08 PM |
Are the maps inside workspace? Or inside of some Instance?
Make sure the maps are in the workspace directly.
If it doesn't work still, then I don't know what's wrong |
|
|
| Report Abuse |
|
|
Vermilius
|
  |
| Joined: 15 Jan 2009 |
| Total Posts: 50 |
|
|
| 10 Apr 2016 08:40 AM |
| The maps are supposed to be stored in serverstorage and copied into workspace when the button is pressed. Additionally, the map currently in the workspace is supposed to be deleted and replaced with the one for the corresponding button. Only 1 map is supposed to be in the workspace at a time. This script copies each map into the workspace but does not remove the current one. So If I press all 4 buttons, all 4 maps are in the workspace. |
|
|
| Report Abuse |
|
|
|
| 10 Apr 2016 10:46 AM |
Use a numerical loop instead of for i, v in pairs
Will soon undergo hibernation to retrieve my lost items in Aura Kingdom. :( |
|
|
| Report Abuse |
|
|
Vermilius
|
  |
| Joined: 15 Jan 2009 |
| Total Posts: 50 |
|
|
| 10 Apr 2016 01:07 PM |
| How can I change it to a numerical loop (not much of a scripter) |
|
|
| Report Abuse |
|
|
cgjnm
|
  |
| Joined: 22 Dec 2011 |
| Total Posts: 2347 |
|
|
| 10 Apr 2016 01:42 PM |
Numerical loop:
local Tab = game.Workspace:GetChildren()
for i = 1, #Tab do if Tab[i]... end |
|
|
| Report Abuse |
|
|
Vermilius
|
  |
| Joined: 15 Jan 2009 |
| Total Posts: 50 |
|
|
| 10 Apr 2016 02:15 PM |
| I was unable to change it to a numerical loop |
|
|
| Report Abuse |
|
|
|
| 10 Apr 2016 02:19 PM |
put all your maps inside a folder inside replicatedstorage named Maps also a little note, your script was pretty weird. from what i read you want to press the button once, make it open and pick a map, and every click from then on changes to the next map until there are no maps, and after there are no maps i made it delete it and turn to unopen
open = false local currentmap = nil maps = game.ReplicatedStorage.Maps script.Parent:WaitForChild("ClickDetector").MouseClick:connect(function() if open == false then open = true currentmap = maps[script.Parent.Var.Value]:Clone() else if script.Parent.Var.Value <= #maps:GetChildren() then currentmap:Destroy() currentmap = maps[script.Parent.Var.Value]:Clone() currentmap.Parent = workspace else currentmap:Destroy() open = false end end |
|
|
| Report Abuse |
|
|
|
| 10 Apr 2016 02:22 PM |
Are you trying to make a random map selector or player voted?
|
|
|
| Report Abuse |
|
|
Vermilius
|
  |
| Joined: 15 Jan 2009 |
| Total Posts: 50 |
|
|
| 10 Apr 2016 02:26 PM |
@master
I'm not sure what the script actually does, so it might be weird as you said. I want the map to spawn based on the button I pressed. So say map2 is out, I want to switch to map1. I press button 1, map2 is deleted and map1 is imported into workspace. I can then switch to any other map by pressing the corresponding button.
That's what's supposed to happen. |
|
|
| Report Abuse |
|
|