Space System Help

I’m trying to make a system where if a player flys high enough they hit a transparent part with CanCollide off, then when they hit that part it hides the baseplate and makes the earth model visible, then when they fly into the earth model, it makes the baseplate visible again and hides the earth model. Now it’s partially working, when I fly and hit the part it hides the baseplate and makes the earth visible, however then it gives me an error.

I have this script in ServerScriptService to create value in the player.

--//Services

--//Instances

--//Data

--//Functions

--//Events

--//Script
game.Players.PlayerAdded:Connect(function(plr)
	local Values = Instance.new("Folder", plr)
	Values.Name = "Values"
	local Location = Instance.new("StringValue", Values)
	Location.Name = "Location"
	Location.Value = "Earth"
end)

I also have this script in ServerScriptService to detect when one of the objects are touched.

--//Services
local RepStorage = game:GetService("ReplicatedStorage")

--//Instances
local Model = workspace.LeaveEarth
local Events = RepStorage:WaitForChild("Events")
local EarthEvent = Events.Planets:WaitForChild("Earth")
local Earth = RepStorage.Planets:WaitForChild("Earth")

--//Data
local db = false
local cd = 4

--//Functions

--//Events

--//Script

--Earth--
for _,LeaveEarth in ipairs(Model:GetDescendants()) do
	if LeaveEarth:IsA("Part") then
		LeaveEarth.Touched:Connect(function(hit)
			if not db then
				db = true
				local HitPart = hit.Parent
				print("Touched!")
				if HitPart:FindFirstChildWhichIsA("Humanoid") then
					print("Is Human")
					local Character = HitPart
					local Player = game.Players:GetPlayerFromCharacter(Character)
					local Values = Player:WaitForChild("Values")
					local Location = Values.Location
					EarthEvent:FireClient(Player, Location)
					wait(cd)
					db = false
				end
			end
		end)
	end
end

for _,EnterEarth in ipairs(Earth:GetDescendants()) do
	if EnterEarth:IsA("MeshPart") then
		EnterEarth.Touched:Connect(function(hit)
			if not db then
				db = true
				local HitPart = hit.Parent
				print("Touched!")
				if HitPart:FindFirstChildWhichisA("Humanoid") then
					print("Is Human")
					local Character = HitPart
					local Player = game.Players:GetPlayerFromCharacter(Character)
					local Values = Player:WaitForChild("Values")
					local Location = Values.Location
					EarthEvent:FireClient(Player, Location)
					wait(cd)
					db = false
				end
			end
		end)
	end
end

--Value Changing
EarthEvent.OnServerEvent:Connect(function(Location, Place)
	Location.Value = Place
end)

Then I have this script in StarterCharacterScripts to make the planet invisible/visible.

--//Services
local RepStorage = game:GetService("ReplicatedStorage")
local Lighting = game:GetService("Lighting")


--//Instances
local Events = RepStorage:WaitForChild("Events")
local EarthFloor = workspace:WaitForChild("Baseplate")
local EarthDetector = workspace:WaitForChild('LeaveEarth')
local Planets = RepStorage:WaitForChild("Planets")
local Detectors = Planets:WaitForChild("Detectors")
local Skys = RepStorage:WaitForChild("Skys")
local SpaceSky = Skys:WaitForChild("SpaceSky")
local EarthSky = Lighting:WaitForChild("EarthSky")
local EarthModel = Planets:WaitForChild("Earth")

--//Data

--//Functions
function Earth(Location, EarthEvent)
	if Location.Value == "Earth" then
		local Place = "Space"
		EarthEvent:FireServer(Location, Place)
		SpaceSky.Parent = Lighting
		EarthSky.Parent = Skys
		EarthModel.Parent = workspace
		EarthFloor.Parent = Planets
		EarthDetector.Parent = Detectors
	elseif Location.Value == "Space" then
		local Place = "Earth"
		EarthEvent:FireServer(Location, Place)
		SpaceSky.Parent = Skys
		EarthSky.Parent = Lighting
		EarthModel.Parent = Planets
		EarthFloor.Parent = workspace
		EarthDetector.Parent = workspace
	end
end

--//Events
local EarthEvent = Events.Planets:WaitForChild("Earth")

--//Script

EarthEvent.OnClientEvent:Connect(function(Location)
	Earth(Location, EarthEvent)
end)

However it gives me this error in the hit detection script

  19:00:17.978  Value is not a valid member of Player "Players.FadedOrder"  -  Server - New:67

This is the line thats erroring,

Location.Value = Place

Any help would really be appreciated.

2 Likes

You need to put local Place out of the function. (My bad, it wasn’t fired by EarthEvent)

OnServerEvent always returns the player who fired as the first argument.

EarthEvent.OnServerEvent:Connect(function(Player, Location, Place)
	Location.Value = Place
end)

The script tries to find .Value in the first argument which is named Location (really the player) and doesn’t find anything. This is the cause of your error.

Thanks, it isn’t erroring anymore. However, it isn’t detecting if the Earth Model was touched. Do you have any idea why this could be happening?

Try printing at the .Touched function for a lil’ check.

I did

for _,EnterEarth in ipairs(Earth:GetDescendants()) do
	if EnterEarth:IsA("MeshPart") then
		EnterEarth.Touched:Connect(function(hit)
			if not db then
				db = true
				local HitPart = hit.Parent
				print("Touched!")
print("Touched")

It doesn’t print anything

What about before the debounce?

That also does not print anything

I am not too sure what is happening here, however i’d try this:

for _,EnterEarth in ipairs(Earth:GetDescendants()) do
	if EnterEarth:IsA("MeshPart") then
		local function TouchEvent(hit)
			print("Touched!")
			-- code
		end
		EnterEarth.Touched:Connect(TouchEvent)

Also, if this isn’t a problem for the game (or experience), why not just check Position.Y of the character, and if it’s above a specific point, run the code. It would also only require a local script to do all of the work and maybe a script to set the value.
.Touched events are pretty stupid and i’d not recommend using them unless there is a specific reason to.

I’m using .Touched and not detecting the y axis is because I am going to add multiple planets, So I need to detect which planet the player is entering/exiting so I can determine which skybox to make visible and everything.

Also, using the code you sent, it still does not work.

You could use :GetTouchingParts() and detect if the HumanoidRootPart of a character is on the table it returns.
Anyway, try making a part in server-side and manually touching the EnterEarth part. Touched only returns if the parts are unanchored as far as I know.