Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
bry0320 10
8 years ago

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

Log in to vote
0
Answered by 8 years ago

All you need to use it this:-

type() -- 

Click here for more info

0
Thank you, good sir. bry0320 10 — 8y
Ad

Answer this question