In this tutorial, we will modulate the player’s size in a cyclic manner. To accomplish this, we will use BindableFunction
and BindableEvent
. Each time the player jumps, a BindableEvent
will be triggered. The total number of jumps will be tracked and made available through a BindableFunction
. The rationale behind using BindableFunction
and BindableEvent
is to facilitate the integration of these events with multiple game functions. In this context, we are only manipulating the player’s size, but these events could also be used to award points for jumping or alter the player’s colors. Hence, our goal is to maximize code reusability. Our code will be divided into two sections: the creation of events and the implementation of events to manipulate the player’s size. Each section will be placed in a separate script.
Part 1: Startup script – Creation of BindableFunction and BindableEvent
In the startup script, we create a BindableFunction and BindableEvent.
BindableFunction is an object in Roblox that allows a function to be invoked across different scripts or instances. It allows for loose coupling, as components don’t need to know about each other and only require the BindableFunction’s name. Its important to know that usage of BindableFunction should be done on the same side of the client-server boundary – either only on client or only on server. If you need to cross this boundary then use RemoteFunction
. BindableFunction
allows to return a value to its caller, this means it should be
BindableEvent, similar to BindableFunction, is a feature that allows an event to be listened for across different scripts or instances.
General way of coding a BindableFunction
is presented below:
local bindableFunction = Instance.new("BindableFunction") bindableFunction.Name = "SomeBindableFunction" -- This is the name of this instance bindableFunction.Parent = humanoid -- Indicates that it will be stored with the humanoid part of our player
You also need to attach to bindableFunction
actual function which will be called:
bindableFunction.OnInvoke = function() return 123 end
Then to call this function in some distant script use this code:
-- Get function instance, its parent was set to humanoid instance of our player, -- so we need to first find player, get its humanoid which will contain our -- BindableFunction instance. All of this is demonstrated in the full source -- code listings later on. local someBindableFunction = humanoid:WaitForChild("SomeBindableFunction") -- To call it use local value = someBindableFunction:Invoke() -- value, after call will be equal to 123
Now, after a short introduction to BindableEvent and its usage, lets implement it in our game. In this script, BindableEvent ‘Jumped’ will fire when the player jumps. Meanwhile, BindableFunction ‘JumpedCount’ will return the total count of jumps when invoked.
local Players = game:GetService("Players") -- Gets the service that handles players in the game function OnPlayerAdded(player: Player) local function OnCharacterAdded(character: Instance) local humanoid = character:FindFirstChildOfClass("Humanoid") -- Looks for a Humanoid instance in the character if humanoid ~= nil then -- Check if a humanoid instance is found local jumpCount = 0 local jumped = Instance.new("BindableEvent") -- Create a BindableEvent jumped.Name = "Jumped" -- Set its name jumped.Parent = humanoid -- Attach it to the humanoid local timesJumped = Instance.new("BindableFunction") -- Create a BindableFunction timesJumped.Name = "JumpedCount" -- Set its name timesJumped.Parent = humanoid -- Attach it to the humanoid humanoid:GetPropertyChangedSignal("Jump"):Connect(function() -- This function runs when the "Jump" property of humanoid changes if humanoid.Jump == true then -- If Jump is true, the player has jumped jumpCount += 1 -- Increment the jumpCount jumped:Fire() -- Fire the jumped event end end) timesJumped.OnInvoke = function() -- The function to be invoked by BindableFunction return jumpCount -- Returns the total jumps the player has made end end end player.CharacterAdded:Connect(OnCharacterAdded) -- Connects to the event that fires when the player's character is added to the game end Players.PlayerAdded:Connect(OnPlayerAdded) -- Connects to the event that fires when a player enters the game
Part 2: GrowAndShrinkPlayer script – Use of BindableFunction and BindableEvent to grow and shrink player
This second script listens to the ‘Jumped’ event and gets the count of jumps using ‘JumpedCount’ function. The size of the player’s character is adjusted according to the number of jumps.
local Players = game:GetService("Players") -- Gets the service that handles players in the game function OnPlayerAdded(player: Player) local function OnCharacterAdded(character: Instance) local humanoid = character:FindFirstChildOfClass("Humanoid") -- Looks for a Humanoid instance in the character if humanoid ~= nil then -- Check if a humanoid instance is found -- Getting the bindable instances local jumped = humanoid:WaitForChild("Jumped") local timesJumped = humanoid:WaitForChild("JumpedCount") if jumped and timesJumped then -- Checks if the bindable instances exist jumped.Event:Connect(function() -- Listens to the jumped event local jumpCount = timesJumped:Invoke() -- Invoke the function to get the jump count local newSize = math.sin(jumpCount/(2*math.pi))+1.5 -- Calculate the new size humanoid.BodyHeightScale.Value = newSize -- Adjust the size of the humanoid end) end end end player.CharacterAdded:Connect(OnCharacterAdded) -- Connects to the event that fires when the player's character is added to the game end Players.PlayerAdded:Connect(OnPlayerAdded) -- Connects to the event that fires when a player enters the game
The second script resizes the player’s character each time the player jumps, using the total count of jumps to determine the new size. This is achieved by a sine function to create a cycle, plus a constant of 1.5 to prevent the player from shrinking too much. Please adjust this factor according to your needs. It is important to note that the player’s character will need a humanoid to exist in the workspace before this script is effective.