How do I aim on object using the mouse on server side?

I am trying to create an object that can be directly oriented from the position of the mouse, only problem is that I also want it to be server side, and since you can’t get the mouse onto server side, as it only exists on the client.

Any help would be and is greatly appreciated.

4 Likes

Remotes can be used here. Send the Position of the Hit of the mouse from the client to the server

1 Like

I’ve tried this before, and the thing is that I want it to be constantly updated, without repeatedly firing the same event over and over, until the key is no longer being pressed.

Then what you could instead do is pass network ownership of the part to the player while the key is pressed.

Just tried that, and when I checked on the server, the orientation hadn’t changed, any other ideas?

You can use CFrame, and While the mouse button 1 is getting pressed, then you can do

local mousedown = false
local part
local mouse = game.Players.LocalPlayer:GetMouse()

mouse.Button1Down:Connect(function()
mousedown = true
while mousedown do
--add the stuff of the orientation you want to do here
part = theparthere
wait()
end
while mousedown do
game.ReplicatedStorage.Event:FireServer(Part, Part.CFrame)
wait(0.3)
end)

--Server Side

game.ReplicatedStorage.Event.OnServerEvent:Connect(function(object, ok)
local tween = game:GetService("TweenService")
tween:Create(object, TweenInfo.new(1), {CFrame = ok}):Play()
end)

This wouldn’t be exactly on the moment but well, you can change the wait to less, but, I don’t know what’s the problem about firing an event over an over? like, you could use a DistanceFromCharacter, for this applies for nearby players, like something like this.

game.ReplicatedStorage.Event.OnServerEvent:Connect(function(object, ok)
for i,v in pairs(game.Players:GetPlayers()) do
if v:DistanceFromCharacter(object.Position) <= 80 then
spawn(function()
local tween = game:GetService("TweenService")
tween:Create(object, TweenInfo.new(1), {CFrame = ok}):Play()
end)
end
end
end)

Your specific solution doesn’t tie towards his request in such a manor he asked for. However, it’d probably be the most suitable as the NetworkOwnership alternative didn’t work. I’d just like to add, that you might want to do server sided sanity checks instead of just trusting the client 100%

So… how would you do it?
there’s no trouble with firing an event over and over until Button1 is up

Like I said, firing the remote repeatedly would be most suitable, I was just stating that it didn’t meet the OP owner’s request.
I’d also like to point out that you’re code is missing an end, and the second while statement will never be fired

Still, it’s kinda complicated too, as, the wait he could do it, it’s using a tool.

Or you could use a tool and .Activated Event.

I am not using a tool, I am using a local script to grab the inputs with a server script to create and do the necessary things to the object.

I would also like to say that this should be 100% possible without using your method, as I’ve seen some games do this, just to name one off the top of my head, Elemental Battlegrounds. There is a specific move that follows the mouse position that anyone on the game can see, as I have tried to attack people and they could tell exactly where it was, and there wasn’t a big delay on the movements from the spell being cast to the part moving to my mouse.

Well, I think you should use my method I suppose

Again, your method is still flawed, if you read my edit there are a few issues.

I made the script here, not on roblox studio, so yes, probably missed a end.

If you would like to see what I mean, I had put the spell and they started moving because they had seen it being casted underneath them.

https://i.gyazo.com/4ebbec85abca101faf923d035e20543c.mp4

Well I mean that still doesn’t address the logic issue with the second while statement never being fired.

the first one will end up causing the second one to yield, until mouse down is already false and never happen.

So this can be done by using the SetNetworkOwnership along with BodyPosition and BodyGyro, just make sure the bodyposition max force is high enough to generate a higher responsiveness.-- Actually you should just be able to do this with setnetworkownership on it’s own as I just removed the body position and body gyro and updated the part’s position and it works fine.

-- Server Code used to test
game.Players.PlayerAdded:Wait()

script.Parent:SetNetworkOwner(game.Players:GetPlayers()[1])

-- Client Code used to test
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
Player.CharacterAdded:Wait()

game:GetService("RunService").RenderStepped:Connect(function()
	workspace.Meep.CFrame = CFrame.new(Mouse.Hit.p)
end)
1 Like

Thank you! This worked perfectly!

1 Like