Need Help Making a Selection System

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.

1 Like

The issue is because you are just grabbing the number value inside this variable planetsselected, not the Instance, so when you increase that variable you are literally changing only a number which is inside the variable, not the real Instance where that number came from.
Hold the Instance IntValue inside the variable, not its value

Do this:

if input.UserInputType == Enum.UserInputType.MouseButton1 then
		local planetsselected = script.Parent:WaitForChild("PlanetsSelected") -- CHANGE IN HERE
		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.Value += 1 --increase # of planets selected -- HERE
						print(planetsselected)
					elseif planet:GetAttribute("Selected") == true then --opposite actions↓
						planet:SetAttribute("Selected", false)
						item.Parent.Selection.Enabled = false
						planetsselected.Value -= 1 -- HERE
						print(planetsselected)
					end
				end
			end
		end
	end
1 Like

It seems to work in the output, as values are changing correctly. However, when I view the Value in properties, it stays 0. When I print the value in the command bar, it also stays 0.

1 Like

That is a client script, it will only change the value on the side of the client, and it will not replicate to server. Perhaps that’s what you are noticing?
Show the code in which you are checking the value too

1 Like

So would I see the value change if I checked the value on the client side? I might just be misunderstanding where the value is changing.