Why is this code using RunService:IsServer()?

I’m having a hard time understanding why we use RunService. to be spesific, RunService:IsServer() in this code. i’m not sure what we’re looking for with IsServer.

context: here’s a module function that adds information to a table to keep track of what pets a player has and it’s being called on server script.

question: why is IsServer important in this situation. what would happen if IsServer wasn’t used.

module function

module.AddPetToInventory = function(player, petName)
	if not isServer then return end
	
	local petId = HttpService:GenerateGUID(true)
	local petInfo = {Model = nil, Name = petName, Id = petId}
	table.insert(module.pets[player], petInfo)
	
	return petInfo
end

server function

function getHeaven(player)
	local leaderstats = player:WaitForChild("leaderstats")
	local gems = leaderstats:FindFirstChild("Gems")
	local petsFolder = player:FindFirstChild("pets")
	
	if gems.Value >= petCost then
	
		gems.Value -= petCost
		local pet = petRarityModule.chooseRandomHeaven()
		print(pet)
		
		local petInfo = petModule.AddPetToInventory(player, tostring(pet))
		
		hatch:FireClient(player, pet, petInfo.Id)
		
		local petString = Instance.new("StringValue")
		petString.Name = tostring(pet)
		petString.Parent = petsFolder
	end
end

All it does is tell return whether the environment is on the server or not. In this specific context, it may be to prevent an exploiter from requiring and running the module on their client. Though the module could also just be placed in ServerStorage anyways, then an exploiter can’t require it. Though I’m not sure exactly why it’s used in this context, just a guess.

yeah, there’s not too many good explanations of it. want to fully understand why it’s being used. is it just for exploiters or are there other parts to it

Typically just for exploiters if it’s placed in a service of which an exploiter could require it.

Not sure where you got this, but sometimes free model malware check if the current running enviroment is in studio or not. They check this so that the malware doesn’t run while playtesting but runs when its in game.

However, looking at the code you provided, there isn’t anything suspicious. The HttpService thing just generates a random string, so I don’t think its malware.

it’s so the player cannot run the function from the client.

1 Like