Is it possible to add values/functions to roblox instances?

I want to be able to do something like

function Player:IsAliveHumanoid()
   local character = self.Character
   if character == nil then return false end
   if character:FindFirstChild("Humanoid") and character:FindFirstChild("HumanoidRootPart") then
		return character.Humanoid.Health > 0
	end
	return false
end

Player:IsAliveHumanoid()

to the roblox player instance

1 Like

how would i add values to objects (player instances) though

The new:
https://developer.roblox.com/en-us/articles/instance-attributes
or the classic value objects (which you parent to an object):
IntValue,
NumberValue,
ObjectValue,
StringValue,
etc.

did you even read my question?

What do you mean values?

You mean like a changing property?

Also yes, it’s possible, you just have to follow the tutorial that @DecodedString just put.
For properties, you can do that too, you just have to treat it like a function, but it wouldn’t be.

I did. Did you even read @DecodedString 's reply? I added alternative ways for values, as you did say
“values” as well. And as @Obj_ective states, you can do more using metatables if desired.

1 Like

im talking about when i do something like,

game.Players.Zulyrus:IsAliveHumanoid()

it will return according to this function

function Player:IsAliveHumanoid()
   local character = self.Character
   if character == nil then return false end
   if character:FindFirstChild("Humanoid") and character:FindFirstChild("HumanoidRootPart") then
		return character.Humanoid.Health > 0
	end
	return false
end

You can make a custom player class

local Player = {}
Player.__index = Player

function Player.new(player)
    return setmetatable({
        _player = player
    }, Player)
end

function Player:IsAliveHumanoid()
    local character = self._player.Character

    if character == nil then return false end
    if character:FindFirstChild("Humanoid") and character:FindFirstChild("HumanoidRootPart") then
		return character.Humanoid.Health > 0
	end

	return false
end

local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
    local customPlayer = Player.new(player)
    print(customPlayer:IsAliveHumanoid())
end)

It might not be the best approach, but it should work.

If you’re trying to add a function so that if you just do

if RandomPlayer:IsHumanoidAlive() then
    print("He do be alive doe")
end

No you can’t, roblox classes are set in stone and can’t be edited

As @Jxl_s said You can make a custom player class
And for your purpose its probably the best approach

Also, if you haven’t already, take a look at @Mega_Hypex & @RuizuKun_Dev 's work in this post of
the thread that @DecodedString provided:

You can get as fancy as you want but you need to really study the metatable metamethods and get
a clear understanding of how they work. So for instance you could make a wrapper / subclass that
would let you use the dot index notation but under the hood it would be using SetAttribute(), which
also could let you “rewrap” an Instance object later, and be able to “re-access” those attribues by
dot indexing them. Not that I recommend doing this, but you could. :slight_smile:
Note: As @anthropomorphic_dev was stating, the Roblox Instance metatables are locked, therefore
we have to do this one-step-away with wrapping to accomplish adding custom values/functions.

Just use a module.

Module:IsAliveHumanoid(game.Players.Zulyrus)