What's a sanity-check? (How do I Implement them?)

TOPIC: What’s a sanity-check? (How do I Implement them?)

Hello fellow devlopers!

It's Braiden, but you can call me Pingu.
Today, I'm super excited to teach you server-sided sanity-checks and why you absolutely should be using them in your game/code. So, let's get started!


Question-1: WHAT?!

A server-side sanity-check is like the safety net for your game.
It's a piece of code that runs on the server rather than the player's device (client).
Its primary job is to validate important actions (like buying/selling) and data (like money/gems) to ensure everything from the player's device (client) is correct and not modified.

Here's a quick image to break it down.

Without a sanity-check…


With a sanity-check…



Question-3: HOW?!

In this sanity-check example, we use a simple magnitude check within the server-side script.
Before allowing a player to equip the sword, we check to ensure they don't attempt to pick up the item from too far away.

function Equipped()	
    --- WHAT'S THIS?
    --- Here we're grabbing our variables we're going to need within this function.
    --- Since we equipped the tool, the tool will be inside the character so we can use 'Tool.Parent' to reference the character-object.
    --- Then use the Players service ( game:GetService('Players') ) to get the player-object from said character-object.
    --- Then use the Player object to reach for humanoid and the torso!
	Character = Tool.Parent
	Player = Players:GetPlayerFromCharacter(Character)
	Humanoid = Character:FindFirstChildOfClass("Humanoid")
	Torso = Character:FindFirstChild("Torso") or Character:FindFirstChild("HumanoidRootPart")

	if not CheckIfAlive() then 
		return 
	end
	
    --- WHAT'S THIS?
    --- This takes the Tool's Handle Position and Torso's Position and check's the magnitude (distance inbetween) and makes sure it's not to high.
    --- If it is, we're going to kick them from the game and return to prevent memory leakage.
	if (Tool.Handle.Position - Torso.Position).Magnitude >= 15 then 
		Player:Kick("Attempted to grab sword from far away!")
		return
	end
	
	ToolEquipped = true
	Sounds.Unsheath:Play()
end

This is just one of the many sanity checks you can add into your server-side scripts to help security and stopping exploits.


Thanks for taking the time to read this forum tutorial!
If you found the information useful, please consider liking the topic and voting in the poll below to share your thoughts.
Best regards,
Braiden (Pingu)


Was this tutorial helpful?

  • Yes
  • Maybe
  • No

0 voters

17 Likes

Nice!
Since it’s a tutorial and many readers may not be really familiar with scripting maybe just add a – comment on the important lines of the script explaining what they do. Some of us still learning scripting would benefit.
Kinda like:

	Player = Players:GetPlayerFromCharacter(Character)  -- makes the variable 'Player' equal the player that equipped the item.
1 Like

Thanks for the feedback!
I will make sure to add code comments, that was actually on my list for this tutorial.
I’m not sure why I forgot about it, thanks for reminding me.

Comment’s got removed?
Kinda wild.