Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Workspace is not a valid member of Players?

checkPoints = game.Workspace:WaitForChild("Checkpoints")
local timer = false
game:WaitForChild("Players")

script.Parent.Touched:Connect(function(hit)
	if timer == false then
		timer = true
		if (hit.Parent:FindFirstChild("Humanoid")) then
			local Player = game.Players[hit.Parent.Name]
			if (Player.leaderstats.Lives.Value > 0) then
				Player.leaderstats.Lives.Value = Player.leaderstats.Lives.Value - 1
			end
			if (Player.leaderstats.Lives.Value <= 0) then
				Player.leaderstats.Lives.Value = 3
				Player.leaderstats.World.Value = 1
				Player.leaderstats.Stage.Value = 1
				Player:LoadCharacter()
			else
				local pSpawn = checkPoints:WaitForChild(tostring(Player.leaderstats.World.Value)):WaitForChild(tostring(Player.leaderstats.Stage.Value)) -- Respawns player
		  		hit.Parent.HumanoidRootPart.CFrame = pSpawn.CFrame
			end
		end
		wait(0.5)
		timer = false
	end
end)

This is my code. I even added a "game:WaitForChild("Players")" at the beginning but that didn't work. Also I should mention i'm new to Lua

3 comments

0

What line is breaking your script?

0

Oh sorry its line 9

0

I should also mention this script works on some objects but not one in particular?

2 answers

✓ Accepted answer

When getting services, the ideal method is to always use game:GetService("ServiceName"). This handy function will always get you the correct service, and if it doesn't exist, create it for you. When dealing with the workspace, however, there is a lovely global already made for you, so you can simply type workspace, and it will already be defined as the game's workspace. Hope that helped!

When getting a player from a character, you want to do the following:

  • Define the Players service (preferably using local Players = game:GetService("Players")
  • Use Players:GetPlayerFromCharacter(model) on the parent of anything that touches your part. It will return nil if no character is found.

1 comment

0

ty so much this fixed it :D

I'm assuming you're using a part and the child of the part is the script. The reason why the error says 'Workspace is not a valid member of Players' is because It goes usually game.Players.LocalPlayer. If you want to get the player, here's how you do it:

script.Parent.Touched:Connect(function(hit)
	if game.Players:GetPlayerFromCharacter(hit.Parent) then
		--Paste your code here
	end
end)

Use the Players:GetPlayerFromCharacter() function if you want to see if what the part touched is a player.

2 comments

0

does this have t be a local script?

0

Yes. When you are using LocalPlayer it has to be a LocalScript.

This discussion is preserved as it was. New answers, comments, and votes are closed.