USER
Can you modify this roblox script and separate the chargetime and stuff?
the ChargeTime is now a NumberValue in replicatedstorage called ChargeTime and the script does not handle the chargetime, it's a module (in the same location as the Bricks module), that module handles when charging should start, when it stops, as well as boosts in chargetime, like increasing chargetime fast or whatever.
Basically the module handles everything chargetime related and can lower or increase the speed of chargetime or lower and increase the chargetime instantly.
Here is a list of all the commands the charge module can do:
StartCharge(speed, maxcharge) - only works if charge is not started and not paused, only one charge can exist for each player
StopCharge() - only works if charge is started or paused
SetChargeSpeed(newspeed) - works if charge is paused, unpaused, stopped, or started, 1 is normal, 2 is doubled, 0.5 is halfed, its a multiplier and only works when charge is started, it does not stack, so if setchargespeed is called 20 times and it is set to 2, it always remains doubled, not 20x speed
PauseCharge() - only works if charge is started and not paused
UnpauseCharge() - only works if charge is paused
IncreaseCharge(amount) - only works if charge is started or paused and unpaused, not capable of going past maxcharge
DecreaseCharge(amount) - only works if charge is started or paused and unpaused, not capable of going below 0
IncreaseChargeIgnoreMax(amount) - only works if charge is started or paused and unpaused, capable of going past maxcharge
RandomizeCharge() - only works if charge is started or paused and unpaused, randomizes charge between 0 and maxcharge
Here is the script right now:
```
-- // SYSTEM \\ --
-- // BrickThrower \\ --
task.wait(0.75)
local plr = game.Players.LocalPlayer
local humanoid = plr.Character:WaitForChild("Humanoid", 6)
local mouse = plr:GetMouse()
local camera = game.Workspace.CurrentCamera
local RS = game:GetService("ReplicatedStorage")
local ThrowEvent = RS:WaitForChild("Throw")
local HighJump = RS:WaitForChild("HighJump")
local Land = RS:WaitForChild("Land")
local UIS = game:GetService("UserInputService")
local runService = game:GetService("RunService")
local gravityForce = Vector3.new(0, game.Workspace.Gravity * plr.Character.HumanoidRootPart.AssemblyMass * 0.95, 0)
local walkspeed = humanoid.WalkSpeed
local jumppower = humanoid.JumpPower
local bodyForce
local debounce = false
local charging = false
local chargeStartTime = 0
local trajectoryUpdateConnection
local trajectoryParts = {}
local maxChargeTime = 3
local maxParts = 50
local chargeSpeed = 0.75
local lastY
local ignoreList = {}
local chargeAnimation = script:WaitForChild("Charge")
local throwAnimation = script:WaitForChild("Throw")
local grabAnimation = script:WaitForChild("Grab")
local ascendAnimation = script:WaitForChild("Ascend")
local landingAnimation = script:WaitForChild("Landing")
local ascendAnimTrack = humanoid:LoadAnimation(ascendAnimation)
local landingAnimTrack = humanoid:LoadAnimation(landingAnimation)
local chargeAnimTrack = humanoid:LoadAnimation(chargeAnimation)
local throwAnimTrack = humanoid:LoadAnimation(throwAnimation)
local grabAnimTrack = humanoid:LoadAnimation(grabAnimation)
chargeAnimTrack.Looped = true
local function calculateTrajectory(startPos, direction, chargeTime)
local trajectory = {}
local velocity = direction.unit * (math.min(chargeTime, maxChargeTime) * 50)
local gravity = Vector3.new(0, -19.81, 0)
local timeStep = 0.1
for i = 1, maxParts do
local time = i * timeStep
local position = startPos + velocity * time + gravity * (time^2 / 2)
table.insert(trajectory, position)
end
return trajectory
end
local folderstorage = Instance.new("Folder")
folderstorage.Name = "Storage"
folderstorage.Parent = workspace
local function createTrajectoryParts()
local parts = {}
for i = 1, maxParts do
local part = Instance.new("Part")
part.Size = Vector3.new(0.2, 0.2, 0.2)
part.Anchored = true
part.CanCollide = false
part.Material = Enum.Material.Neon
part.Parent = folderstorage
table.insert(parts, part)
end
return parts
end
local function handleLanding()
debounce = true
local rootPart = plr.Character:FindFirstChild("HumanoidRootPart")
task.delay(.15, function()
rootPart.Anchored = true
end)
landingAnimTrack:Play()
local function onLanding()
local rocksmodule = require(RS.Modules:WaitForChild("RocksModule"))
rocksmodule.Ground(plr.Character:WaitForChild("HumanoidRootPart").Position - Vector3.new(0, 2, 0), 7, Vector3.new(4, 1, 4), nil, 10, false, 4)
Land:FireServer("Hard")
end
onLanding()
landingAnimTrack.Stopped:Connect(function()
rootPart.Anchored = false
debounce = false
end)
end
local function updateTrajectoryParts(parts, trajectory, chargeTime)
local colorStart = Color3.fromRGB(90, 90, 255)
local colorEnd = Color3.fromRGB(255, 53, 56)
local chargePercent = math.min(chargeTime / maxChargeTime, 1)
local trajectoryLength = #trajectory
for i, part in ipairs(parts) do
if i > trajectoryLength then
part.Transparency = 1
else
local partColor = colorStart:lerp(colorEnd, chargePercent)
part.Color = partColor
if i == 1 then
part.Position = plr.Character["Right Arm"].Position
else
local previousPartPos = trajectory[i-1]
local currentPos = trajectory[i]
local midPoint = (previousPartPos + currentPos) / 2
local size = (previousPartPos - currentPos).Magnitude
part.Size = Vector3.new(0.1, 0.1, size)
part.CFrame = CFrame.new(midPoint, currentPos)
end
end
end
end
local function throwBrick(chargeTime)
charging = false
debounce = true
ThrowEvent:FireServer(mouse.Hit.p, math.min(chargeTime, maxChargeTime))
throwAnimTrack:Play()
chargeAnimTrack:Stop()
ascendAnimTrack:Stop()
task.wait(1)
end
humanoid.StateChanged:Connect(function(oldState, newState)
if newState == Enum.HumanoidStateType.Landed and oldState == Enum.HumanoidStateType.Freefall then
local verticalVelocity = plr.Character.HumanoidRootPart.AssemblyLinearVelocity.Y
if verticalVelocity < -110 then
handleLanding()
else
Land:FireServer("Soft")
end
end
end)
UIS.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
if not debounce and not charging then
local verticalVelocity = plr.Character.HumanoidRootPart.AssemblyLinearVelocity.Y
if verticalVelocity > 35 then
HighJump:FireServer(plr.Character)
ascendAnimTrack:Play()
end
charging = true
chargeStartTime = tick()
trajectoryParts = createTrajectoryParts()
chargeAnimTrack:Play(1)
humanoid.WalkSpeed = 8.5
humanoid.JumpPower = 0
-- Initialize body force
bodyForce = Instance.new("BodyForce")
bodyForce.Force = Vector3.new(0, 0, 0)
bodyForce.Parent = plr.Character.HumanoidRootPart
local initialAntiGravityFactor = verticalVelocity > 35 and 0.95 or 0.6
trajectoryUpdateConnection = runService.RenderStepped:Connect(function()
local chargeTime = (tick() - chargeStartTime) * chargeSpeed
local direction = (mouse.Hit.p - plr.Character.HumanoidRootPart.Position).unit
local startPos = plr.Character["Right Arm"].Position
local trajectory = calculateTrajectory(startPos, direction, chargeTime)
updateTrajectoryParts(trajectoryParts, trajectory, chargeTime)
local speed = math.min(chargeTime / maxChargeTime, 3)
chargeAnimTrack:AdjustSpeed(speed)
local rootPart = plr.Character:FindFirstChild("HumanoidRootPart")
if rootPart and charging then
local lookAt = CFrame.lookAt(rootPart.Position, mouse.Hit.p)
rootPart.CFrame = rootPart.CFrame:Lerp(lookAt, 0.075)
local antiGravityFactor = math.max(1 - chargeTime / maxChargeTime, 0)
bodyForce.Force = gravityForce * antiGravityFactor * initialAntiGravityFactor
end
end)
end
end
end)
UIS.InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
if bodyForce then
bodyForce:Destroy()
end
if charging and not debounce then
charging = false
humanoid.WalkSpeed = walkspeed
humanoid.JumpPower = jumppower
game.Players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart").Anchored = false
local chargeTime = (tick() - chargeStartTime) * chargeSpeed
for _, part in ipairs(folderstorage:GetChildren()) do
part:Destroy()
end
trajectoryParts = {}
throwBrick(chargeTime)
if trajectoryUpdateConnection then
trajectoryUpdateConnection:Disconnect()
end
task.delay(0.2, function()
grabAnimTrack:Play()
task.wait(0.9)
debounce = false
end)
end
end
end)
```ASSISTANT
Certainly! I'll modify the script to use a separate module to handle the charge time as per your specifications. The module will manage the charge mechanics, and the script will interact with it using the provided commands. Here's the updated script:
```lua
-- // SYSTEM \\ --
-- // BrickThrower \\ --
task.wait(0.75)
local plr = game.Players.LocalPlayer
local humanoid = plr.Character:WaitForChild("Humanoid", 6)
local mouse = plr:GetMouse()
local camera = game.Workspace.CurrentCamera
local RS = game:GetService("ReplicatedStorage")
local ThrowEvent = RS:WaitForChild("Throw")
local HighJump = RS:WaitForChild("HighJump")
local Land = RS:WaitForChild("Land")
local UIS = game:GetService("UserInputService")
local runService = game:GetService("RunService")
local gravityForce = Vector3.new(0, game.Workspace.Gravity * plr.Character.HumanoidRootPart.AssemblyMass * 0.95, 0)
local walkspeed = humanoid.WalkSpeed
local jumppower = humanoid.JumpPower
local bodyForce
local debounce = false
local charging = false
local trajectoryUpdateConnection
local trajectoryParts = {}
local maxChargeTime = 3
local maxParts = 50
local chargeSpeed = 0.75
local lastY
local ignoreList = {}
-- Require the ChargeModule
local ChargeModule = require(RS.Modules:WaitForChild("ChargeModule"))
local ChargeTimeValue = RS:WaitForChild("ChargeTime")
local chargeAnimation = script:WaitForChild("Charge")
local throwAnimation = script:WaitForChild("Throw")
local grabAnimation = script:WaitForChild("Grab")
local ascendAnimation = script:WaitForChild("Ascend")
local landingAnimation = script:WaitForChild("Landing")
local ascendAnimTrack = humanoid:LoadAnimation(ascendAnimation)
local landingAnimTrack = humanoid:LoadAnimation(landingAnimation)
local chargeAnimTrack = humanoid:LoadAnimation(chargeAnimation)
local throwAnimTrack = humanoid:LoadAnimation(throwAnimation)
local grabAnimTrack = humanoid:LoadAnimation(grabAnimation)
chargeAnimTrack.Looped = true
local function calculateTrajectory(startPos, direction, chargeTime)
local trajectory = {}
local velocity = direction.unit * (math.min(chargeTime, maxChargeTime) * 50)
local gravity = Vector3.new(0, -19.81, 0)
local timeStep = 0.1
for i = 1, maxParts do
local time = i * timeStep
local position = startPos + velocity * time + gravity * (time^2 / 2)
table.insert(trajectory, position)
end
return trajectory
end
local folderstorage = Instance.new("Folder")
folderstorage.Name = "Storage"
folderstorage.Parent = workspace
local function createTrajectoryParts()
local parts = {}
for i = 1, maxParts do
local part = Instance.new("Part")
part.Size = Vector3.new(0.2, 0.2, 0.2)
part.Anchored = true
part.CanCollide = false
part.Material = Enum.Material.Neon
part.Parent = folderstorage
table.insert(parts, part)
end
return parts
end
local function handleLanding()
debounce = true
local rootPart = plr.Character:FindFirstChild("HumanoidRootPart")
task.delay(.15, function()
rootPart.Anchored = true
end)
landingAnimTrack:Play()
local function onLanding()
local rocksmodule = require(RS.Modules:WaitForChild("RocksModule"))
rocksmodule.Ground(plr.Character:WaitForChild("HumanoidRootPart").Position - Vector3.new(0, 2, 0), 7, Vector3.new(4, 1, 4), nil, 10, false, 4)
Land:FireServer("Hard")
end
onLanding()
landingAnimTrack.Stopped:Connect(function()
rootPart.Anchored = false
debounce = false
end)
end
local function updateTrajectoryParts(parts, trajectory, chargeTime)
local colorStart = Color3.fromRGB(90, 90, 255)
local colorEnd = Color3.fromRGB(255, 53, 56)
local chargePercent = math.min(chargeTime / maxChargeTime, 1)
local trajectoryLength = #trajectory
for i, part in ipairs(parts) do
if i > trajectoryLength then
part.Transparency = 1
else
local partColor = colorStart:lerp(colorEnd, chargePercent)
part.Color = partColor
if i == 1 then
part.Position = plr.Character["Right Arm"].Position
else
local previousPartPos = trajectory[i-1]
local currentPos = trajectory[i]
local midPoint = (previousPartPos + currentPos) / 2
local size = (previousPartPos - currentPos).Magnitude
part.Size = Vector3.new(0.1, 0.1, size)
part.CFrame = CFrame.new(midPoint, currentPos)
end
end
end
end
local function throwBrick(chargeTime)
charging = false
debounce = true
ThrowEvent:FireServer(mouse.Hit.p, math.min(chargeTime, maxChargeTime))
throwAnimTrack:Play()
chargeAnimTrack:Stop()
ascendAnimTrack:Stop()
task.wait(1)
end
humanoid.StateChanged:Connect(function(oldState, newState)
if newState == Enum.HumanoidStateType.Landed and oldState == Enum.HumanoidStateType.Freefall then
local verticalVelocity = plr.Character.HumanoidRootPart.AssemblyLinearVelocity.Y
if verticalVelocity < -110 then
handleLanding()
else
Land:FireServer("Soft")
end
end
end)
UIS.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
if not debounce and not charging then
local verticalVelocity = plr.Character.HumanoidRootPart.AssemblyLinearVelocity.Y
if verticalVelocity > 35 then
HighJump:FireServer(plr.Character)
ascendAnimTrack:Play()
end
charging = true
-- Start charging using the module
ChargeModule.StartCharge(chargeSpeed, maxChargeTime)
trajectoryParts = createTrajectoryParts()
chargeAnimTrack:Play(1)
humanoid.WalkSpeed = 8.5
humanoid.JumpPower = 0
-- Initialize body force
bodyForce = Instance.new("BodyForce")
bodyForce.Force = Vector3.new(0, 0, 0)
bodyForce.Parent = plr.Character.HumanoidRootPart
local initialAntiGravityFactor = verticalVelocity > 35 and 0.95 or 0.6
trajectoryUpdateConnection = runService.RenderStepped:Connect(function()
-- Get the current charge time from the NumberValue
local chargeTime = ChargeTimeValue.Value
local direction = (mouse.Hit.p - plr.Character.HumanoidRootPart.Position).unit
local startPos = plr.Character["Right Arm"].Position
local trajectory = calculateTrajectory(startPos, direction, chargeTime)
updateTrajectoryParts(trajectoryParts, trajectory, chargeTime)
local speed = math.min(chargeTime / maxChargeTime, 3)
chargeAnimTrack:AdjustSpeed(speed)
local rootPart = plr.Character:FindFirstChild("HumanoidRootPart")
if rootPart and charging then
local lookAt = CFrame.lookAt(rootPart.Position, mouse.Hit.p)
rootPart.CFrame = rootPart.CFrame:Lerp(lookAt, 0.075)
local antiGravityFactor = math.max(1 - chargeTime / maxChargeTime, 0)
bodyForce.Force = gravityForce * antiGravityFactor * initialAntiGravityFactor
end
end)
end
end
end)
UIS.InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
if bodyForce then
bodyForce:Destroy()
end
if charging and not debounce then
-- Stop charging using the module
ChargeModule.StopCharge()
charging = false
humanoid.WalkSpeed = walkspeed
humanoid.JumpPower = jumppower
game.Players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart").Anchored = false
-- Get the final charge time from the NumberValue
local chargeTime = ChargeTimeValue.Value
for _, part in ipairs(folderstorage:GetChildren()) do
part:Destroy()
end
trajectoryParts = {}
throwBrick(chargeTime)
if trajectoryUpdateConnection then
trajectoryUpdateConnection:Disconnect()
end
task.delay(0.2, function()
grabAnimTrack:Play()
task.wait(0.9)
debounce = false
end)
end
end
end)
```
**Explanation of Changes:**
- **ChargeModule:** At the beginning of the script, I've required the `ChargeModule` which handles all charge-related functionalities.
```lua
local ChargeModule = require(RS.Modules:WaitForChild("ChargeModule"))
local ChargeTimeValue = RS:WaitForChild("ChargeTime")
```
- **Removed Local Charge Handling:** I've removed all local variables and functions that handle charge time calculations using `tick()` and `chargeStartTime`.
- **Using ChargeModule in InputBegan:**
- When the player presses the mouse button, we start charging using the `ChargeModule.StartCharge(speed, maxcharge)` function.
- Set `charging` to `true` for script state management.
```lua
UIS.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
if not debounce and not charging then
-- Existing code...
charging = true
-- Start charging using the module
ChargeModule.StartCharge(chargeSpeed, maxChargeTime)
-- Existing code...
end
end
end)
```
- **Using ChargeTimeValue in Trajectory Update:**
- In the `RenderStepped` function, I've replaced the local charge time calculation with `local chargeTime = ChargeTimeValue.Value`.
- This ensures that we're always using the charge time managed by the `ChargeModule`.
```lua
trajectoryUpdateConnection = runService.RenderStepped:Connect(function()
-- Get the current charge time from the NumberValue
local chargeTime = ChargeTimeValue.Value
-- Existing code...
end)
```
- **Using ChargeModule in InputEnded:**
- When the player releases the mouse button, we stop charging using the `ChargeModule.StopCharge()` function.
- Get the final charge time from `ChargeTimeValue.Value`.
- Reset `charging` state.
```lua
UIS.InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
if bodyForce then
bodyForce:Destroy()
end
if charging and not debounce then
-- Stop charging using the module
ChargeModule.StopCharge()
charging = false
-- Existing code...
-- Get the final charge time from the NumberValue
local chargeTime = ChargeTimeValue.Value
-- Existing code...
end
end
end)
```
- **Removed Redundant Variables:**
- Removed `chargeStartTime` as it's no longer needed.
- `chargeSpeed` is now primarily used when starting the charge with the module.
- **Ensured Module Handles Charge Adjustments:**
- The module is responsible for any charge time speed adjustments or modifications.
- If you need to change the charge speed during charging, you can use `ChargeModule.SetChargeSpeed(newspeed)` accordingly.
**Note:** Make sure that the `ChargeModule` correctly updates the `ChargeTime` NumberValue in `ReplicatedStorage` so that the script can read the current charge time from it.
This modification ensures that all charge-related logic is handled by the `ChargeModule`, and the script interacts with it using the provided commands. The script now reads the current charge time from the `ChargeTime` NumberValue and updates the trajectory and animations accordingly.