|
| 19 Jul 2017 12:55 AM |
So I have 3 StringValues that store the current weapons you have equipped. I made a trading system that, when both players accept, will trade each player's items. If they had one of the items they traded equipped, it'll set the value to blank. For example, let's say we had the values - ["Sword", "Rifle", "RPG"] and the player traded the rifle, the values would look like this - ["Sword", "", "RPG"]. After this is finished, I want the values to move left to leave the blank values at last, so I want to make an algorithm that moves the non-empty values to the left, empty values to the right. I thought of just moving each value to the left once and making the last one blank, but then I realized if the first two values were blank, the last value would only get moved once and the first value would be blank.
I know this is a large explanation for such a simple thing, but I just want to know a simple function that would allow me to do this efficently, as I like keeping my game organized.
-- roblox chat syst## |
|
|
| Report Abuse |
|
|
| |
|
ghulty
|
  |
| Joined: 21 Mar 2011 |
| Total Posts: 42 |
|
|
| 19 Jul 2017 01:04 AM |
I am not fully sure I understand the problem correctly or how weapons are stored, but for basic pseudo code, at least (some may be hashed in which case I will upload)
local weapon_values = { stringvalue1, stringvalue2, stringvalue3 }
function removeWeapon(wname) for i,v in pairs(weapon_values) do if v.Value == wname then
-- Here, if, e.g., the rifle is traded in that list, i == 2 -- so since only the ones after it (until 3) need to be shifted...
for n = i, (#weapon_values - 1) do weapon_values[n].Value = weapon_values[n + 1].Value -- each starting at the one being removed is set to the one to the right end
weapon_values[#weapon_values].Value = "" -- last blank
end end end
I may have made some specific error but that's how I would approach it at least. |
|
|
| Report Abuse |
|
|
ghulty
|
  |
| Joined: 21 Mar 2011 |
| Total Posts: 42 |
|
|
| 19 Jul 2017 01:06 AM |
| Oops you would want to break after it sets the last to blank so if there are two of the same type it wouldn't do weird things |
|
|
| Report Abuse |
|
|
|
| 19 Jul 2017 01:07 AM |
Sadly I tried this already, but I already got something in mind. Thanks for trying to help though.
-- roblox chat syst## |
|
|
| Report Abuse |
|
|
ghulty
|
  |
| Joined: 21 Mar 2011 |
| Total Posts: 42 |
|
|
| 19 Jul 2017 01:09 AM |
| I can almost guarantee that is the simplest basic design to do what you're trying to do unless I misunderstood, and I posted it less than 1 minute after your last post... But if you wish to do it another way I understand I guess. |
|
|
| Report Abuse |
|
|
|
| 19 Jul 2017 01:14 AM |
Yes I know what you tried doing, but the way it's supposed to do it is only run the remove thing once, not for each time it removes something.
-- roblox chat syst## |
|
|
| Report Abuse |
|
|
| |
|
ghulty
|
  |
| Joined: 21 Mar 2011 |
| Total Posts: 42 |
|
|
| 19 Jul 2017 04:33 AM |
| That made... absolutely no sense. I am not sure if ^ is expressing equal lack of understanding or just farming posts but you are abusing pronouns to where I, and most likely others who read this, have no clue what you're talking about. If you got it figured out that's fine, but otherwise I strongly recommend rewording the original issue. |
|
|
| Report Abuse |
|
|