Hello devs,
I am trying to create a system in which I can select and deselect Planets in the client.
I used a script which checks the number of planets, represented by a NumValue. I am trying to increase and decrease that value by 1 each time I select a planet.
--workspace.Planets.Planet1:SetAttribute("Selected", true)
local userinputservice = game:GetService("UserInputService")
local camera = workspace.CurrentCamera
local RS = game:GetService("RunService")
local item = nil
local player = script.Parent.Parent
local function MouseRaycast()
local mousepos = userinputservice:GetMouseLocation()
--print(mousepos)
local mouseray = camera:ViewportPointToRay(mousepos.x, mousepos.Y)
--print(mouseray)
local rayresult = workspace:Raycast(mouseray.Origin, mouseray.Direction * 1000)
--print (rayresult)
return rayresult
end
userinputservice.InputBegan:Connect(function(input, processed)
local result = MouseRaycast()
if result and result.Instance then
item = result.Instance
else item = nil
end
if processed then -- not necessarily necessary
return
end
--Where the problem is↓
-----------------------------
if input.UserInputType == Enum.UserInputType.MouseButton1 then
local planetsselected = script.Parent:WaitForChild("PlanetsSelected").Value
if item ~= nil then
local planet = item.Parent.Parent
if item:IsA("BasePart") then
if planet:GetAttribute("Team") == player.Team.Name then
if planet:GetAttribute("Selected") == false then
planet:SetAttribute("Selected", true) --select the planet
item.Parent.Selection.Enabled = true --visually select it
planetsselected += 1 --increase # of planets selected
print(planetsselected)
elseif planet:GetAttribute("Selected") == true then --opposite actions↓
planet:SetAttribute("Selected", false)
item.Parent.Selection.Enabled = false
planetsselected -= 1
print(planetsselected)
end
end
end
end
end
end)
I’m completely stumped on how to make this work, as when I check in studio the value stays 0, even when I directly change the value from studio and then try clicking a planet again. I even ran commands to print the value, and it always prints 0 no matter how many planets I select.
Also, there are no errors in the output, as the code just creates its own local value. It’s just not doing what I want it to do, which is change the value in the workspace.