hI so this script works and it shows the e promt like its supposed to but when it appears and i press e it does not reset my Power why is this
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local UserInputService = game:GetService("UserInputService")
local PowerBar = script.Parent.PowerBar.Bar
local MaxPower = 100
local IsPromptActive = false
PowerBar.Size = UDim2.new(0, 0, 1, 0) -- Start with an empty power bar
local humanoid = Character:WaitForChild("Humanoid")
humanoid:SetAttribute("Power", 0) -- Start with 0 power
local function GiveTool()
-- Create and give the player a tool (replace this with your actual tool creation logic)
local tool = Instance.new("Tool")
tool.Name = "YourToolName"
tool.RequiresHandle = true
tool.RequiresCursor = true
tool.Parent = Player.Backpack
end
local function ShowPrompt()
local ButtonPrompt = Instance.new("TextLabel")
ButtonPrompt.Name = "ButtonPrompt"
ButtonPrompt.Size = UDim2.new(0, 100, 0, 50)
ButtonPrompt.Position = UDim2.new(0.5, -50, 0.8, 0)
ButtonPrompt.BackgroundColor3 = Color3.new(0, 0, 0)
ButtonPrompt.BackgroundTransparency = 0.5
ButtonPrompt.BorderSizePixel = 0
ButtonPrompt.Text = "[E]"
ButtonPrompt.TextColor3 = Color3.new(1, 1, 1)
ButtonPrompt.TextSize = 24
ButtonPrompt.Parent = script.Parent
local promptAction
promptAction = UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if input.KeyCode == Enum.KeyCode.E and not gameProcessedEvent then
local currentPower = humanoid:GetAttribute("Power")
if currentPower >= MaxPower and not IsPromptActive then
humanoid:SetAttribute("Power", 1) -- Reset Power to 0
PowerBar.Size = UDim2.new(0, 0, 1, 0)
ButtonPrompt:Destroy()
IsPromptActive = false
GiveTool()
promptAction:Disconnect() -- Disconnect the input listener
end
end
end)
IsPromptActive = true
end
local function UpdatePowerBar()
while true do
local currentPower = humanoid:GetAttribute("Power")
if currentPower < MaxPower then
humanoid:SetAttribute("Power", currentPower + 1) -- Increment power by 1
local newScale = currentPower / MaxPower
PowerBar.Size = UDim2.new(newScale, 0, 1, 0)
elseif currentPower >= MaxPower and not IsPromptActive then
ShowPrompt()
end
wait(0.1) -- Adjust the delay to control the rate of power increase
end
end
spawn(UpdatePowerBar)
3 Likes
Let’s look closely at this function:
local function ShowPrompt()
local ButtonPrompt = Instance.new("TextLabel")
ButtonPrompt.Name = "ButtonPrompt"
ButtonPrompt.Size = UDim2.new(0, 100, 0, 50)
ButtonPrompt.Position = UDim2.new(0.5, -50, 0.8, 0)
ButtonPrompt.BackgroundColor3 = Color3.new(0, 0, 0)
ButtonPrompt.BackgroundTransparency = 0.5
ButtonPrompt.BorderSizePixel = 0
ButtonPrompt.Text = "[E]"
ButtonPrompt.TextColor3 = Color3.new(1, 1, 1)
ButtonPrompt.TextSize = 24
ButtonPrompt.Parent = script.Parent
local promptAction
promptAction = UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if input.KeyCode == Enum.KeyCode.E and not gameProcessedEvent then
local currentPower = humanoid:GetAttribute("Power")
if currentPower >= MaxPower and not IsPromptActive then
humanoid:SetAttribute("Power", 1) -- Reset Power to 0
PowerBar.Size = UDim2.new(0, 0, 1, 0)
ButtonPrompt:Destroy()
IsPromptActive = false
GiveTool()
promptAction:Disconnect() -- Disconnect the input listener
end
end
end)
IsPromptActive = true
end
At the end of this function there is IsPromptActive
variable being set to true
. This will run just after running the function meaning that this variable will store true
as soon as the function runs, even before the player presses E on his keyboard. Now look at the if statement in the UserInputService
event:
if currentPower >= MaxPower and not IsPromptActive then
There is the problem. Before the player presses E, IsPromptActive
is being set to true
, meaning that the statement mentioned above will never be true.
Edit: I can edit the code to make for you easier to understand what I mean.
1 Like
so i just change that line of code to false
1 Like
im new to scripting so i dont really get that
i only started 4 days ago this is my first script
1 Like
Well this is impressive if this is your 4th day of scripting. I will edit the function so it will be easier for you to understand what’s the issue.
1 Like
thank you right now i am working on a battlegrounds game and it has turned out really well so far i would love for the game to do well but it is no where near ready and i suck at thumbnail design.
https://www.roblox.com/games/14596273084/Combat-BattleGrounds
Remove “not
” from it and tell me if this works. This idea randomly came to my mind. I will show what I mean anyway because it might not be the solution.
so just remove not nothing else
It works thank you so much!! Char limit
also i have a tool in replicated storage it is supposed to give you when you press e why is it not doing that
Use a RemoteEvent and give the tool to the player using a normal script
I don’t see anything else giving tool to the player and there’s an error in this script:
RequiresCursor is not a valid member of Tool "YourToolName"
Are you sure that tools have RequiresCursor
property?
Also as @T3_MasterGamer said, I suggest making a system, where the server gives the tool to the player.
i replaced your tool name with Serious Punch
It won’t work. Show me the whole path to the “Serious Punch” tool in the Explorer.
I also have a question: Does the tool do something only on client side? If no, we need to make a server give the tool to the player.
1 Like
so the tool is in a folder and thats it i have it so that it gives the player that tool when they press e when the bar is charged
Here’s an updated function:
local function GiveTool()
game:GetService("ReplicatedStorage").Serious.Tool["Serious Punch"]:Clone().Parent = Player.Backpack
end
Keep in mind that the client will only see the tool and it might work only on his side. To make the tool appear for others, you need to use a remote event.
what lines do i replace this with