Hey all! I have successfully injected my own code into the Roblox studio tour gyro balls for the new game I am making that is like mario kart and monkey ball fused together.
The code I had altered is within the Occupant script and gave it a new module script for “abilties” players can get. It works very nicely but I am still very basic minded when it comes to scripting so I would like some feedback on what can be improved and better organized or made more efficient.
local CollectionService = game:GetService("CollectionService")
local RunService = game:GetService("RunService")
local Workspace = game:GetService("Workspace")
local enterBall = require(script.enterBall)
local exitBall = require(script.exitBall)
local abilities = require(script.ability)
local ball = script.Parent.Parent.Parent
local driverSeat = ball.DriverSeat
local enterPrompt = ball.EnterPrompt
local exitPrompt = ball.ExitPrompt
local abilityPrompt = ball.AbilityPrompt
local sounds = ball.DriverSeat.Sounds
local fx = ball.DriverSeat
local basepart = ball.Exterior.CollisionBall
local abilnumber = ball.DriverSeat.AbilityType
-- If Ball speed is less than this value and occupant exists, exit prompt will appear
local EXIT_PROMPT_SPEED_THRESHOLD = 1
local MIN_TIME_BEFORE_EXIT_PROMPT_SHOWN = 2
local exitPromptConnection
-- Get any "disable exiting ball" parts
local disableExitBallBoundsParts = CollectionService:GetTagged("DisableExitingBall")
-- Listen for conditions that change whether the exit prompt is enabled
local function trackExitPromptEnabled(): RBXScriptConnection
local connection = RunService.Heartbeat:Connect(function()
-- check if ball is in the disable exit prompt bounds
local isBallInDisableExitPromptBounds = false
for _, disableExitBallBoundsPart in disableExitBallBoundsParts do
local partsInBounds =
Workspace:GetPartBoundsInBox(disableExitBallBoundsPart:GetPivot(), disableExitBallBoundsPart.Size)
for _, part in partsInBounds do
if part:IsDescendantOf(ball) then
isBallInDisableExitPromptBounds = true
break
end
end
end
-- check if ball speed is slow enough to exit the ball
local isBallSpeedLow = false
local ballVelocity = ball.PrimaryPart.AssemblyLinearVelocity
if ballVelocity.Magnitude < EXIT_PROMPT_SPEED_THRESHOLD then
isBallSpeedLow = true
end
-- enable exit prompt if ball spped is slow enough and ball is not in the disable exit prompt bounds
if isBallSpeedLow and not isBallInDisableExitPromptBounds then
exitPrompt.Enabled = true
else
exitPrompt.Enabled = false
end
end)
return connection
end
local function onOccupantChanged()
local humanoid = driverSeat.Occupant
enterPrompt.Enabled = humanoid == nil
-- Exit prompt will initially be false regardless of the occupancy state
exitPrompt.Enabled = false
if humanoid then
-- Delay allows player to explore controlling the ball without immediately being prompted to exit
task.delay(MIN_TIME_BEFORE_EXIT_PROMPT_SHOWN, function()
exitPromptConnection = trackExitPromptEnabled()
end)
else
if exitPromptConnection then
exitPromptConnection:Disconnect()
exitPromptConnection = nil
end
end
end
------The code I injected below-------------
basepart.Touched:Connect(function(other)
if other.Name == "MysteryBox" and driverSeat.abilityget.Value == false then
local humanoid = driverSeat.Occupant
driverSeat.abilityget.Value = true
sounds["Space Chimes"]:Play()
fx.GeneralAbilities.Stars.Enabled = true
task.wait(4)
sounds["Enemy Gadget - Brawl Stars SFX"]:Play()
driverSeat.abilityget.Value = true
task.wait(1)
fx.GeneralAbilities.Stars.Enabled = false
local number = math.random(1,5)
task.wait()
abilnumber.Value = number
if number == 1 then
abilityPrompt.ObjectText = "You got: Overload Ability!"
elseif number == 2 then
abilityPrompt.ObjectText = "You got: Damage Ring Ability!"
elseif number == 3 then
abilityPrompt.ObjectText = "You got: Fire Ball Ability!"
elseif number == 4 then
abilityPrompt.ObjectText = "You got: Ice Cube Ability!"
elseif number == 5 then
abilityPrompt.ObjectText = "You got: SUPER Ability!"
end
abilityPrompt.Enabled = true
end
end)
local function initialize()
driverSeat:GetPropertyChangedSignal("Occupant"):Connect(onOccupantChanged)
enterPrompt.Triggered:Connect(enterBall)
exitPrompt.Triggered:Connect(exitBall)
abilityPrompt.Triggered:Connect(abilities)
end
initialize()