My client ability script isnt working!

So my ability system is not working. What it does is that it checks for the player’s selected boots, then looks through a table to find the ability. Then it will require the module script that’s name is similar to the ability. When i switch my selected boots, it still does inf jump when the ability is double jump!

My Script :

local attributes = {
	["Default"] = {
		["Ability"] = "DoubleJump",
	},
	["Developer"] = {
		["Ability"] = "InfJump",
	},
}

local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")

function abilityUse(ability)
	
	require(ability)
	
end

player:WaitForChild("SelectedBoots"):GetPropertyChangedSignal("Value"):Connect(function()
	
	if attributes[player:WaitForChild("SelectedBoots").Value] then
		
		local ability = script[attributes[player:WaitForChild("SelectedBoots").Value].Ability]
		abilityUse(ability)

	end
	
end)

abilityUse(script[attributes[player:WaitForChild("SelectedBoots").Value].Ability])

The issue you’re facing is that the abilities are not changing correctly when the selected boots change. To fix this issue, you should clear the abilities when the selected boots change and then set the new ability.
Here’s an example:

local attributes = {
    ["Default"] = {
        ["Ability"] = "DoubleJump",
    },
    ["Developer"] = {
        ["Ability"] = "InfJump",
    },
}

local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")

local currentAbility = nil

function abilityUse(ability)
    if currentAbility then
        currentAbility:Destroy() -- Clear the previous ability
    end
    
    currentAbility = require(ability)
end

player:WaitForChild("SelectedBoots"):GetPropertyChangedSignal("Value"):Connect(function()
    if attributes[player:WaitForChild("SelectedBoots").Value] then
        local ability = script[attributes[player:WaitForChild("SelectedBoots").Value].Ability]
        abilityUse(ability)
    end
end)

-- Initialize with the default ability
abilityUse(script[attributes["Default"].Ability])

This script assumes that your ability scripts handle their functionality, including enabling and disabling when necessary.

I got an error message saying, ‘Workspace.haydengamingdo.AbilityHandler:18: attempt to call missing method ‘Destroy’ of table’

Here’s an example of one of the abilities :

local userInputs = game:GetService('UserInputService')
local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")

--local anim = script:WaitForChild("Animation") --Double Jump Anim
--local loadedAnim = humanoid:LoadAnimation(anim)

local Max_Jumps = 2
local Time_Between_Jumps = 0.2
local numJumps = 0
local canJumpAgain = false

local ability = {}

local function onStateChanged(oldState, newState)
	
	if Enum.HumanoidStateType.Landed == newState then
		
		--loadedAnim:Stop()
		numJumps = 0
		canJumpAgain = false
		
	elseif Enum.HumanoidStateType.Freefall == newState then
		
		task.wait(Time_Between_Jumps)
		canJumpAgain = true
		
	elseif Enum.HumanoidStateType.Jumping == newState then
		
		canJumpAgain = false
		numJumps += 1
		
	end
	
end

local function onJumpRequest()
	
	if canJumpAgain and numJumps < Max_Jumps then
		
		--loadedAnim:Play()
		humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
		
	end
	
end

humanoid.StateChanged:Connect(onStateChanged)
userInputs.JumpRequest:Connect(onJumpRequest)

return ability

Ok so, I changed

currentAbility:Destroy()

to

currentAbility = nil

and it does not work either…

Basically you need to ensure:

  1. Attributes Table:
  • The code uses a table called attributes to associate different types of boots with specific abilities. For example, it links “Default” boots with the “DoubleJump” ability and “Developer” boots with “InfJump.” You can customize this table to include more boot-ability pairs as needed.
  1. Player and Character Setup:
  • It sets up references to the player and their character’s humanoid in the Roblox game.
  1. Ability Management Functions:
  • The script tracks the current ability and its module using currentAbilityModule and currentAbility variables.
  • The abilityUse function handles the activation and deactivation of abilities. It first disables the current ability (if there is one) and then loads and activates the new ability based on the specified module name.
  • If the ability module cannot be found, a warning message is displayed.
  1. Event Handling for Boot Selection:
  • The script listens for changes in the selected boots via the “Value” property of the player’s “SelectedBoots.”
  • When the player switches boots, it checks if the selected boots are defined in the attributes table and calls the abilityUse function with the corresponding ability module name.
  1. Sample Ability Module (DoubleJump):
  • There’s an example module structure provided for the “DoubleJump” ability. Similar modules should be created for other abilities your game requires.
  • Each module should contain Enable and Disable functions, where you can define the specific behavior for enabling and disabling the ability.

Please give me a remade ability handler script based on what you wrote

local attributes = {
    ["Default"] = {
        ["Ability"] = "DoubleJump",
    },
    ["Developer"] = {
        ["Ability"] = "InfJump",
    },
}

local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local currentAbilityModule = nil  -- store the currently loaded ability module

function abilityUse(abilityModuleName)
    -- unload the previous ability module if one exists
    if currentAbilityModule then
        currentAbilityModule:Destroy()
    end

    -- load and use the new ability module
    local abilityModule = script:FindFirstChild(abilityModuleName)
    if abilityModule then
        currentAbilityModule = abilityModule:Clone()
        currentAbilityModule.Parent = character
    end
end

player:WaitForChild("SelectedBoots").PropertyChanged:Connect(function()
    local selectedBoots = player:WaitForChild("SelectedBoots").Value
    if attributes[selectedBoots] then
        local ability = attributes[selectedBoots].Ability
        abilityUse(ability)
    end
end)

-- load the initial ability when the script starts
local initialAbility = attributes[player:WaitForChild("SelectedBoots").Value].Ability
abilityUse(initialAbility)

Wait, I don’t see where you are requiring the currentAbilityModule to start up the module.

Hello? Is anyone there? I hope so…

I ended up fixing it, the way I did it was that in the module script, I added a table variable that holds all the names of that the boot uses, and checks if the player’s selected boots are the same as one of the items in the table. Thank you to everyone that tried to help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.