Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to check if an argument passed to a function is a string, object or any other DataType?

I am making a function for days now that finds a Instance and then returns it. I've simplified to change a player's score to a number specified by an argument.

Here is the function in script and ModuleScript form (Requires leaderstats and a IntValue called 'Points' parented under it.):

SCRIPT VERSION:

function GivePlayerSomePoints(plr,num)
	local player;
	if (tostring(plr)) then
		player = game.Players[plr];
	elseif (plr.ClassName) then
		player = plr;
	else
		warn("Player doesn't exist.");
		return nil; -- Stop function.
	end;
	if (player.leaderstats.Points) then
		if (num) then
			if (tonumber(num)) then
				player.leaderstats.Points.Value = player.leaderstats.Points.Value + num;
			end;
		end;
	end;
end;

wait(30); -- Wait until player object exists.
GivePlayerSomePoints(YOURNAME,50);

MODULESCRIPT VERSION

-- In a ModuleScript
local test = {}

function test:GivePlayerSomePoints(plr,num)
	local player;
	if (tostring(plr)) then
		player = game.Players[plr];
	elseif (plr.ClassName) then
		player = plr;
	else
		warn("Player doesn't exist.")
		return nil; --Stop function.
	end;
	if (player.leaderstats.Points) then
		if (num) then
			if (tonumber(num)) then
				player.leaderstats.Points.Value = player.leaderstats.Points.Value + num;
			end;
		end;
	end;
end;

return test;
-- In a script
wait(30)
local ModuleScript = require(game.Workspace.ModuleScript)
ModuleScript:GivePlayerSomePoints(YOURNAME),50)

I can post my original function if wanted.

1 answer

This discussion is preserved as it was. New answers, comments, and votes are closed.