So im trying to make a script that grants players a random superpower upon pressing a button
i tried making the superpower as an item that gives you the powers when you equip it (i mean it appears in the backpack but you cant physically hold it)
the problem is that i made a code that should speed you up once you “equip” the speed superpower but for some reason it does not work
local character = player.Character -- Gets the Players Character
local humanoid = character:FindFirstChild("Humanoid") -- Gets the Humanoid
player.Backpack.speed.Equipped:Connect(function() -- speed is the name of the superpower
if humanoid then -- Checks if its the Humanoid
humanoid.WalkSpeed = 32 -- Doubles the Speed of the Player
end
end)
player.Backpack.speed.Unequipped:Connect(function()
if humanoid then -- Checks if its the Humanoid
humanoid.WalkSpeed = 16 -- The Normal Speed of The Player
end
end)
It does appear in the backpack but once you use it it doesn’t change anything
Can the problem be cause of the scripts location?
the current location of the script is in Replicated Storage in the speed tool and its a local script
if not then i really have no idea why isnt it working
basically i just want to make a speed coil that doesn’t show up in your hand
hopefully yall can point me in the right direction
local player = game:GetService("Players").LocalPlayer
local character = player.Character -- Gets the Players Character
local humanoid = character:FindFirstChild("Humanoid") -- Gets the Humanoid
local tool = script.Parent
tool.Equipped:Connect(function() -- speed is the name of the superpower
if humanoid then -- Checks if its the Humanoid
humanoid.WalkSpeed = 32 -- Doubles the Speed of the Player
end
end)
tool.Unequipped:Connect(function()
if humanoid then -- Checks if its the Humanoid
humanoid.WalkSpeed = 16 -- The Normal Speed of The Player
end
end)
Hmm, still doesn’t work
ok lets start from the beginning. Where do i store items so they dont automatically appear in the players backpack, but appear there only after certain criteria is met?
Well you need to clone it into your backpack which means after you press the button it will clone it to your character. The ReplicatedStorage is just there for you to keep a main copy so that you can clone it multiple times.
The whole code operates on this system
i have a table with powers
the powers themselves are just tools, with LocalScript in it, that are originally stored in replicated storage
then when the player presses the specific button on the keyboard (E for example) it takes a random power from the table, makes a clone of it and stores it into players backpack
in my example its the speed power that speeds you up when you equip it
it does everything right except for the script in the tool. It does store the power in the players backpack but when equipped, it does nothing
Hmm that is very strange, it worked fine for me on my end.
I changed the speed to 64 to make it easier to see the speed difference
I changed the code so that it matches what you said (Pressing E instead of pressing a text button).
What about putting the tool in the player in Workspace instead of Players.Backpack:
My test code(placed in StarterPlayerScripts)
local Button = script.Parent;
local RS = game:GetService("ReplicatedStorage");
local player = game:GetService("Players").LocalPlayer;
local UIS = game:GetService("UserInputService");
UIS.InputBegan:Connect(function(input: InputObject, gameProcessedEvent: boolean)
if gameProcessedEvent then return; end
if input.KeyCode == Enum.KeyCode.E then
local powerClone = RS.Powers.speed:Clone(); -- clones the tool from the replicatedstorage
powerClone.Parent = player.Character; -- gives to the player
player.Character.Humanoid:UnequipTools()
print("E has been pressed and given the tool!")
end
end)
-- Tool in workspace version, uncomment below and comment above to test
--[[ <--- Remove this to test
UIS.InputBegan:Connect(function(input: InputObject, gameProcessedEvent: boolean)
if gameProcessedEvent then return; end
if input.KeyCode == Enum.KeyCode.E then
local powerClone = RS.Powers.speed:Clone(); -- clones the tool from the replicatedstorage
powerClone.Parent = workspace:FindFirstChild(player.Name); -- gives to the player in workspace
player.Character.Humanoid:UnequipTools()
print("E has been pressed and given the tool in workspace!")
end
end)
--]]--
Well yes, you can just directly give the speed buff instead of making it a tool
So it would look something like
local UIS = game:GetService("UserInputService");
local player = game:GetService("Players").LocalPlayer;
local character = player.Character -- Gets the Players Character
local humanoid = character:FindFirstChild("Humanoid") -- Gets the Humanoid
UIS.InputBegan:Connect(function(input: InputObject, gameProcessedEvent: boolean)
if gameProcessedEvent then return; end
if input.KeyCode == Enum.KeyCode.E then
humanoid.WalkSpeed = 32;
end
end)
If you want to add a timer just use something like task.wait()
local UIS = game:GetService("UserInputService");
local player = game:GetService("Players").LocalPlayer;
local character = player.Character -- Gets the Players Character
local humanoid = character:FindFirstChild("Humanoid") -- Gets the Humanoid
UIS.InputBegan:Connect(function(input: InputObject, gameProcessedEvent: boolean)
if gameProcessedEvent then return; end
if input.KeyCode == Enum.KeyCode.E then
humanoid.WalkSpeed = 32; -- give speed
task.wait(4) -- wait 4 seconds
humanoid.WalkSpeed = 16; -- reset back
end
end)
Add UserInputService, detect if a key is being pressed (your choice) and ensure the player has the tool in his backpack, and when input has ended then make it normal again
The problem is that I want the player to get 1 random power. That’s why I store them in the power folder and also have a dedicated table from where it takes 1 random power and gives it to the character
So I can’t just change their speed once they press the button.
If I recorrect my question:
How to make a speed coil that doesn’t show up in your hand when you equip it, but gives its speed effect