Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to Select Unit Movement in a RTS style?

Asked by 3 years ago

Goal: I would like to select a player created troop and move the troop to the mouse.Target smoothly.

This is what I have currently:

Local Script - Highlights the first selected part [1] of the array and sends the first part over to the server, this is what I want sent.


local function HightlightPart(part) local box = table.remove(Boxes.Cache, 1) if not box then box = Instance.new("SelectionBox") box.LineThickness = 0.05 box.Adornee = part box.Parent = workspace end box.Adornee = part table.insert(Boxes.InUse, box) end local function UnhightlightPart(part) local position, box for i,selection in pairs(Boxes.InUse) do if selection.Adornee == part then position = i box = selection end end if box then box.Adornee = nil table.insert(Boxes.Cache, table.remove(Boxes.InUse, position)) end end mouse.Button1Down:Connect(function() local SelectMultiple = UserInputService:IsKeyDown(Enum.KeyCode.LeftShift) local Target = mouse.Target local MousePos = mouse.Hit.p local check = table.find(Selected, Target) if Target and Target.Parent then if Selected then MousePosServerTransfer:FireServer(Selected[1], MousePos) UnhightlightPart(part) table.remove(Selected, check) end if SelectMultiple then if check then UnhightlightPart(Target) table.remove(Selected, check) else HightlightPart(Target) table.insert(Selected, Target) end elseif not check then HightlightPart(Target) table.insert(Selected, Target) end elseif not SelectMultiple then for _,part in pairs(Selected) do UnhightlightPart(part) end table.clear(Selected) end end)

Server Script - Makes the part move to the mouse position of the client.

RP:WaitForChild("Client"):WaitForChild("Remotes"):WaitForChild("UnitMove").OnServerEvent:Connect(function(plr, part, MousePos)
    print("Recieved:", MousePos)
    part.Position = MousePos
    part.CFrame = CFrame.new(MousePos)
end)

All the variables are sorted out. Please comment if you want me to elaborate, sorry if it is messy.

This is the best version I can produce but if anyone can improve the script or give me an explanation, please help, thanks.

Answer this question