-
What do you want to achieve? Keep it simple and clear!
I want the “Selected” BoolValue to be toggled when a unit is clicked on. I want to then use this BoolValue to restrict whether or not the player can move the unit. -
What is the issue? Include screenshots / videos if possible!
I try to change the “Selected” value in my script, but it returns with an error that states “Selected is not a valid member of Model ‘Workspace.Unit’.” From my end, both on the client and server, however, “Selected” is under Workspace.Unit. I can’t figure out why the script thinks that the BoolValue doesn’t exist under the Unit.
-
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I searched on the DevFourm for similar issues, but I couldn’t find any solutions that applied to my issue. Most of them were people making typos which hopefully isn’t the case here since I’ve checked multiple times… That would be embarrassing.
Here is my code, I will separate both scripts which are relevant into separate codeblocks. I’m a novice scripter, so my code is likely very messy. Any tips on how I can improve my code are also very appreciated!
PathfindingScript (server script)
local PathfindingService = game:GetService("PathfindingService")
local humanoid = script.Parent:WaitForChild("Humanoid")
local torso = script.Parent:WaitForChild("Torso")
local goalEvent = game:GetService("ReplicatedStorage"):WaitForChild("goalPos")
local pawnteam = script.Parent:WaitForChild("Team").Value
script.Parent:WaitForChild("Torso").BrickColor = pawnteam
local function newGoal(player, position, target)
if player.TeamColor == pawnteam then
if target.Parent:FindFirstChild("HumanoidRootPart") ~= nil then
if target.Parent.Selected == true then
target.Parent.Selected = false
else
target.Parent.Selected = true
end
else
if target.Parent.Selected == true then
local path = PathfindingService:CreatePath()
path:ComputeAsync(torso.Position, position)
local waypoints = path:GetWaypoints()
for i, waypoint in pairs(waypoints) do
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
end
end
end
end
goalEvent.OnServerEvent:Connect(newGoal)
DetectMousePos (local script)
local player = game:GetService("Players").LocalPlayer
local goalPos = game:GetService("ReplicatedStorage"):WaitForChild("goalPos")
local mouse = player:GetMouse()
mouse.Button1Down:Connect(function()
goalPos:FireServer(mouse.Hit.Position, mouse.Target)
end)