How to prevent players from picking up items near their spawn location?

Basically I have a game where you can pick up items you find around the map. I also save the location of where each player leaves so when they rejoin they spawn at the location they left at.

My goal is to make it so if they spawn near items that they cannot pick them up. How should I go about doing this? I was thinking of saving their location as a cframe value and checking magnitude each time they pick up an item but wouldn’t that be too expensive to pull off? (There can be tons of items in one spot at times)

1 Like

How does this spawn system work?

Edit: Ah I just noticed this part. I was going to say just have it not spawn where you have spawns hah.

1 Like

Maybe you can try a cooldown that blocks the player from picking up items. Maybe have a datastore that saves a boolean, after 10 seconds or so this boolean switches to true (so they can pick up again), and the spawn system (I assume its a on touched thing) checks this datastore value.

1 Like

the items u click to get them.
are you saying I should make it so people cant pickup items for a set amount of time on spawning? That could be a potential workaround.

although I would prefer to make it so they cant pickup items near their spawn forever.
Magnitude checks seems the only way right now but idk if that will lag the server

Ah, clicking should be the same either way heh. Doesn’t matter how the tool is picked up, for the idea to work it just needs to be able to check the datastore.

I’m not familiar with this so I can’t say if it will, but I hope the suggestion for a cooldown helped. Hopefully someone else here can answer this. Good luck with your project! It sounds cool.

1 Like

Adding on to this, I did a quick search of magnitudes and it doesn’t seem like it would create serious performance issues. I think the magnitude idea would work, but anyone feel free to correct me if my one minute research is wrong heh.

1 Like

My game already does a lot of magnitude checks and there hasn’t been any problems so far. But I’d still like confirmation just to be safe. Appreciate the help.

1 Like

It’s possible to do the magnitude check on the client to prevent the server from having to check in the first place. But keep the server-side magnitude check-in place as well, just in case a creative client decides to still trigger the event.

1 Like

regardless its checking magnitude on the server everytime though if someone is picking up an item not near their spawn location. Is it still safe to do? There will be a ton of items around the map. You can expect an item to be clicked at least once every few seconds. This wont cause any server lag?

Are you checking from the server by default? To do a magnitude check on the client you’d need to run a local script that listens to the events for the item. Then if the magnitude check is alright, you would fire off a remote event to tell the server the player wants to pick up the item. The server would then also do a magnitude check and if everything checks out it will then pick up the item.

1 Like

Yes I’m aware of that but lets put it this way.
it only checks on the client and prevents a server check if the item is near their spawn location. If it isn’t then it will still check on the server regardless.

Well yeah, it’s going to check regardless that’s the point. But the extra check on the client is what would help for the performance since if the item is near the spawn location a packet is never sent to the server to begin with.

Though this performance gain may be negligible, though it should be a bit more noticeable with higher item and player counts. I wouldn’t recommend skipping the magnitude check on the server since you’d never want to trust the client.

1 Like

My main question is would this still cause lag at all?
Also instead of doing a client check on the client in my case it’d be best for me to put a local script inside of the player that constantly gets rid of any items that spawn near the spawn location. That way the player cant view the items. (Because in my game certain items that spawn could be rare. A player could just server hop and wait until they find a rare item then have their friend join to pick it up who isn’t close by)

If you’d like to avoid the magnitude altogether, you could probably use a object for instance or maybe the spatial query API. Which could simply disable item pickup for a specific player server-side if they’re within a region or part. This would cut multiple checks down to one initial check when the player enter the area and one last check when leaving the area similar to a part’s Touched and TouchEnded events.

Another solution could be to simply cache their location when they enter the server, and disable item-pickup until they’re x amount of studs from their initial spawn point in which case they could return and get the items anyway.

As far as it causing lag, it would depend on your implementation. If you’re running it every frame then there’s bound to be some sort of performance hit. But if it’s event-based only then the magnitude would only be checked when the event is triggered. Personally speaking I don’t think the performance here is anything to worry about unless you’re actually checking the magnitude every frame which I don’t recommend.

2 Likes

Which would be better performance wise on the server?
Check magnitude each time someone tries to pickup an item.
or check every second using getpartsboundinbox and if the player is within the boundary picking up items will be disabled and if they aren’t they will be allowed to pickup items.

I definitely don’t recommend checking every second. It’d be better to check it each time someone tries picking up the item if we’re comparing only those two scenarios.

Though the downside to that is, no one can pick up items where they spawned period unless you disable that check for them.

1 Like

they aren’t supposed to pickup items where they spawned no matter what.
So would it be better to do a partsboundinbox check as opposed to a magnitude check if its doing the check everytime someone tries to pickup an item?

Between the two the bounding box would be more expensive. (this is my assumption, though I may not be entirely correct). Since the position data is just vectors that should be available to you via the object’s properties.

Though both are viable options.

1 Like

Alright thanks for all the help! Greatly appreciated!

1 Like