Effective ways to implement "SnapToGrid" with GUIs?

Context: I’m working on making an inventory.

How would I go about making (dynamically draggable/moves as the mouse moves) GUIs “snap to grid”? I’ve thought of rounding the UDim2 down upon mouse release, although, that leaves the vulnerability of “unsnapped” positional data being saved to Datastores if the server shut down/player left without letting go of their mouse (i.e. AltF4-ing/running out of battery).

This could not only be a visual bug, but also allow the player to hold an extra item in their inventory. (Yes, I know I’m being a bit pedantic.)

Are there any better ways, or ways that could solve this issue; is this the best option?

1 Like

If you’re handling the gui locally which I assume you are for the mouse position then just don’t replicate the changes until the person releases their mouse that way when the data is saved it won’t see the unsnapped position

1 Like

Sorry I didn’t make it clear in my post (I’ve editted it now). My GUIs move with mouse movement.

2 Likes

Can you not save the rounded data to the datastore?

3 Likes

Thanks! This thought never occured to me.

3 Likes

Actually the only problem is if it clips with another item which could make two items in one spot or some other issue so make sure you check for that before just saving the rounded position

2 Likes

and if there’s another item what happens then? Also if I were you I’d put this in a module script to prevent cluttering your data store script

2 Likes

Actually it would be impossible to make it fully anti-exploitable, since local changes to GUI replicate across to the server. Unless I have a server-sided script that manages/validates these movements, which requires many remoteevents, and puts noticeable strain on the server. Hmm…

Perhaps I could check for the amount of items in a player’s inventory on the server upon item addition?

what will the player exploit? the amount of items or the type? because you don’t need to know those to change the item’s position in the inventory all you need to do is find the item’s current location then change it to the new one

If an exploiter changed the locations of the items in their inventory, then some grid sectors could be left unused, and others cramped between these grid slots (by directly changing the Position property. This means that the player can technically hold infinite items in their inventory.

The only way, I believe, to avoid this from happening is to let these GUIs be purely aesthetics (since after all, they are controlled by the client) and check for the amount of GUIs in the inventory from the server (upon ChildAdded).

well im not sure the way you’ve structured the inventory data is efficient

Its the best I can think of. If you’ve got any better ideas, feel free to share.