Hello devs!
I am creating a RTS game and i got a problem with moving the unit anywhere, Instead of the mouse position, The unit goes to the origin of the world and runs away from the player.
localscript:
--Services and Mouse
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
--Modules & Remotes
local Modules = game.ReplicatedStorage.Modules
local Remotes = game.ReplicatedStorage.RemoteEvents
--Requires
local UnitManager = require(Modules.UnitManager)
--Script
local SelectedUnit = nil
Remotes.AddSelectedUnit.OnClientEvent:Connect(function(unit)
local CheckifUnit = false
if unit then
CheckifUnit = unit:FindFirstChild("isselected")
end
if CheckifUnit then
SelectedUnit = unit
else
warn("COULD NOT SELECT, NOT A UNIT.")
end
print(SelectedUnit)
end)
Mouse.Button1Down:Connect(function()
if not (SelectedUnit == nil) then
local mousepos = Vector3.new(Mouse.Hit.p)
UnitManager.MoveUnit(SelectedUnit,mousepos)
else
print("No unit selected.")
end
end)
modulescript:
local Units = game.ReplicatedStorage.Units
local module = {}
module.SpawnUnit = function(unit,vectorposition)
local UnitC = Units[unit]:Clone()
UnitC.Parent = workspace
UnitC:MoveTo(Vector3.new(vectorposition))
end
module.MoveUnit = function(unit,vectorposition)
local SeeifThereisaHumanoid = unit:FindFirstChild("Humanoid")
local CheckifUnit = unit:FindFirstChild("isselected")
if CheckifUnit then
if SeeifThereisaHumanoid then
unit.Humanoid:MoveTo(vectorposition)
else
error("Unit ".. unit.Name .." has no humanoid")
end
else
error("Entity/Object ".. unit.Name .." is not a Unit.")
end
end
return module