Validating Data
Technically, messages are just regular RemoteEvent
/ RemoteFunction
calls, meaning that exploiters can send invalid data to function that run on server.
To prevent that, WCS provides a way to attach validators to message arguments in your message config.
Validator is a regular function that accepts an argument
and returns a boolean
value.
We'll be using a library called t
in this example to validate arguments.
To attach validators to a message, you need to specify them inside your message config:
- TypeScript
- Luau
attack.ts
import { Skill, SkillDecorator } from "@rbxts/wcs";
import { t } from "@rbxts/t";
@SkillDecorator
export class Attack extends Skill {
@Message({
Type: "Event",
Destination: "Client",
Validators: [t.number, t.string],
})
protected printSomething(a: number, b: string) {
print(a,b);
}
}
attack.lua
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local WCS = require(ReplicatedStorage.WCS)
local t = require(ReplicatedStorage.t)
local Attack = WCS.RegisterSkill("Attack")
function Attack:printSomething(a,b)
print(a,b)
end
WCS.DefineMessage(Attack.printSomething, {
Type = "Event",
Destination = "Client",
Validators = {t.number, t.string},
})
return Attack
Each index in the Validators
array corresponds to a function argument. From now, message calls that contain invalid arguments will be rejected.