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

teleport player after leaderstat value is reached?

hi, im making an RPG based game. i have the leaderboard value of "lives" that tells the player how many lives they have. im trying to work out a script so that once the value of "Lives" reaches 0, it'll teleport the player to another area. heres what i got so far

local p = game.Players:GetChildren()
local stats = p:findFirstChild("leaderstats")       
local sp = stats:findFirstChild("Lives") 
if sp == nil then return false end                             
if (sp.Value == 0) then 
	for i = 1, #p do
		p[i].Character:MoveTo(Vector3.new(-37206.949, 516.942, -9.089))
		
	end
	end

this obviously doesnt work, but im trying to figure out how i should write to so that it'll teleport the players based on the Lives value. im also getting an error on line 2 for " attempt to call a nil value". what should i do?

1 answer

✓ Accepted answer
local Players = game:GetService("Players")


local TeleportCoordinates = CFrame.new(Vector3.new(-37206.949, 516.942, -9.089))


for _, Player in ipairs(Players:GetPlayers()) do
	---------------
	local LivesLeaderstat = Player.leaderstats.Lives
	local LivesUpdateConnection;
	---------------
	LivesUpdateConnection = LivesLeaderstat:GetPropertyChangedSignal("Value"):Connect(function()
		if (LivesLeaderstat.Value <= 0) then
			---------------
			LivesUpdateConnection:Disconnect()
			---------------
			local Character = Player.Character or Player.CharacterAdded:Wait()
			Character:SetPrimaryPartCFrame(TeleportCoordinates)
		end
	end)
end

1 comment

0

this worked perfectly! only issue is after the player has died at "0" they get teleported to the right place, but will respawn and stay in the original area

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