I created a post on Code Review roughly an hour ago here because I wanted to improve my code. However, after further testing I can pretty much say my code is very, very buggy the way it is.
I’ve tried using ApplyImpulse()
, straight up setting the AssemblyLinearVelocity
of the part instead of BodyVelocity, but those seem to make the part do nothing at all instead. I’m obviously very uneducated on both the new velocity system. I’m unsure of what to change at this point.
Here’s my code:
hockeypuck.rbxl (57.2 KB)
ServerScriptService/PuckHandler
local Puck = workspace.Puck
local DefaultOrientation = Puck.CFrame
Puck.BodyGyro.CFrame = DefaultOrientation
--//SERVICE VARIABLES
local Players = game:GetService("Players")
----
local connection
local lastHit
local Debounce = false
Puck:SetNetworkOwner(nil)
local function followPlayer()
connection = Puck.HitDetection.Touched:Connect(function(hit)
local Character = hit:FindFirstAncestorOfClass("Model")
local Player = Players:GetPlayerFromCharacter(Character)
if Player then
if Debounce == true and Player.Name == lastHit then
print(Player.Name.." cannot come in control of the puck again yet.")
else
connection:Disconnect()
Puck:SetAttribute("Controller",Character.Name)
Puck.CanCollide = false
Puck:PivotTo((Character.HumanoidRootPart.CFrame * CFrame.new(0,-2.75,-2)) * CFrame.Angles(0, math.rad(90), math.rad(-90)))
local WeldConstraint = Instance.new("WeldConstraint")
WeldConstraint.Name = "PuckWeld"
WeldConstraint.Part0 = Character.HumanoidRootPart
WeldConstraint.Part1 = Puck
WeldConstraint.Parent = Character.HumanoidRootPart
Puck.BodyGyro:Destroy()
game:GetService("ReplicatedStorage").Remotes.PuckUpdate:FireClient(Player)
end
end
end)
end
local function breakWeld()
local Player = Players:FindFirstChild(lastHit)
local Character = Player.Character
local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
local WeldConstraint = HumanoidRootPart:FindFirstChild("PuckWeld")
WeldConstraint:Destroy()
Puck.CanCollide = true
local bg = Instance.new("BodyGyro")
bg.Name = "BodyGyro"
bg.D = 500
bg.MaxTorque = Vector3.new(400000,400000,400000)
bg.P = 3000
bg.CFrame = DefaultOrientation
bg.Parent = Puck
----
followPlayer() --resets the script
----
end
game.ReplicatedStorage.Remotes.PuckUpdate.OnServerEvent:Connect(function(plr,angle) --Puck has been shot!
lastHit = plr.Name
Debounce = true
----
----
local bv = Instance.new("BodyVelocity")
bv.MaxForce = Vector3.new(math.huge, 1000, math.huge)
bv.P = math.huge
bv.Parent = Puck
bv.Velocity = angle --propels the puck
breakWeld()
wait()
bv:Destroy()
wait(0.5)
Debounce = false
end)
followPlayer()
StarterPlayerScripts/LocalScript
local Puck = workspace.Puck
--//SERVICE VARIABLES
local ContextActionService = game:GetService("ContextActionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Power = 0
local ChargingUp = false
local function Shoot(actionName, inputState, inputObject)
if inputState == Enum.UserInputState.Begin then
ChargingUp = true
repeat
wait()
Power += 5
until Power == 100 or ChargingUp == false
else
ChargingUp = false
game:GetService("ReplicatedStorage").Remotes.PuckUpdate:FireServer(workspace.CurrentCamera.CFrame.LookVector*Power)
print(workspace.CurrentCamera.CFrame.LookVector*Power)
Power = 0
ContextActionService:UnbindAction("Shoot")
end
end
local function bindShoot()
ContextActionService:BindAction("Shoot",Shoot,true, Enum.UserInputType.MouseButton1, Enum.KeyCode.ButtonR2)
ContextActionService:SetTitle("Shoot","Shoot")
ContextActionService:SetPosition("Shoot", UDim2.new(1, -160, 0, 40))
end
ReplicatedStorage.Remotes.PuckUpdate.OnClientEvent:Connect(function()
bindShoot()
end)
It would mean a lot for someone to tell me what I’m doing wrong!