hockeypuck.rbxl (57.2 KB)
I wrote this code last night for a hockey-themed sports game I’m working on. However… it’s a little stutter-y. I’m not completely sure if it’s my code or the Roblox physics engine causing the stutter. Even if it’s not my fault (which I’m inclined to believe it is), I still would love to optimize the scripts all around.
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)
Any feedback would be greatly appreciated!