so i have this script:
target.CFrame = CFrame.new(target.Position,mouse.Hit.Position)
it dosn’t go where it looks like it is pointing even though it looks like it, how do i fix this?
so i have this script:
target.CFrame = CFrame.new(target.Position,mouse.Hit.Position)
Can you provide more info and if possible a visual of the whole script
i doubt it would help but yeah, heres the script on the client that handles this
game:GetService("RunService").Heartbeat:Connect(function()
if target and target.CanGrab.Value == script.Parent.Name then
local pos = hrp.Position + (mouse.Hit.Position - hrp.Position).Unit * 10
target.BodyPosition.Position = pos
target.CFrame = CFrame.new(target.Position,mouse.Hit.Position)
target.BodyGyro.CFrame = target.CFrame
end
end)
server
plr.PlayerStuff.Grabbed.Value = true
local distance = (plr.Character.HumanoidRootPart.Position - target.Position).Magnitude
if distance > 30 then return end
target.CanGrab.Value = plr.Name
if not target:FindFirstChild("BodyPosition") then
local bp = Instance.new("BodyPosition", target)
bp.D =300
bp.MaxForce = Vector3.new(10000, 10000, 10000)
end
if not target:FindFirstChild("BodyGyro") then
local bg = Instance.new("BodyGyro", target)
bg.D = 300
bg.MaxTorque = Vector3.new(10000, 10000, 10000)
end
target.Anchored = false
target:SetNetworkOwner(plr)
The current code creates BodyPosition
and BodyGyro
components within the target
object if they don’t exist. This is a good practice. However, consider setting their properties at the same time when you create them to ensure consistency.
It’s unclear what plr.PlayerStuff.Grabbed.Value
is doing, but you should ensure that network ownership is correctly managed. Set the network ownership of the grabbed object to the player using target:SetNetworkOwner(plr)
.
Server Script:
if not target:FindFirstChild("BodyPosition") then
local bp = Instance.new("BodyPosition", target)
bp.D = 300
bp.MaxForce = Vector3.new(10000, 10000, 10000)
bp.P = 10000 -- Proportional value for more responsive movement
end
Instead of setting the target.CFrame
, target.BodyPosition.Position
, and target.BodyGyro.CFrame
separately, you can set both the position and rotation using the CFrame
itself. This can help in ensuring that the object’s orientation is aligned with its movement.
Client (Assuming):
if target and target.CanGrab.Value == script.Parent.Name then
local pos = hrp.Position + (mouse.Hit.Position - hrp.Position).Unit * 10
target.CFrame = CFrame.new(pos, mouse.Hit.Position)
end
Network ownership is crucial for maintaining a fair and consistent game experience in multiplayer games, as it prevents players from directly manipulating objects they shouldn’t have control over and ensures that actions are properly validated by the server.