How to make script stop when player is in a given area?

I’m trying to get the player’s “time” to stop rising when they’re in the safezone. I have tried to figure this out myself but everything I’ve tried has failed. All help is appreciated!

Script:

local myDataStore = game:GetService("DataStoreService"):GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local level = Instance.new("IntValue")
	level.Name = "Time"
	level.Parent = leaderstats

	local data = nil

	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(player.UserId.."-time")
	end)

	if success then
		level.Value = (data or 0)
	else
		warn("There was an error when attempting to load your data.\n"..errormessage)
	end

	while task.wait(1) do
		level.Value += 1
	end
end)

game:GetService('Players').PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			print(player.Name .. " has died!")
			player.leaderstats.Time.Value = 0
		end)
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-level", player.leaderstats.Level.Value)
	end)


	if success then
		print("Player Data has been saved!")
	else
		warn("There was an error while trying to save data.\n"..errormessage)
	end
end)

Workspace:
image

Sorry for the messy script.

All responses are welcome; thanks!! :smiley:

1 Like

Is the spawnZone something you stand on or is it like an invisible zone?


Note: The SafeZone script is not the script I showed above. It’s something different. The Script I’m trying to fix is in ServerScriptService.

Thanks for the responce!

1 Like

Sorry I’m not very sure. I believe you could do it with either the :IsTouching() function or by creating a region3, both of which I have no experience with. I’d recommend doing research on both of those as well and find the solution that would be best for you game. Best of luck!

1 Like
local myDataStore = game:GetService("DataStoreService"):GetDataStore("myDataStore")
local players = game:GetService("Players")
local coreSpawn = workspace:WaitForChild("CoreSpawn")
local spawnZone = coreSpawn:WaitForChild("SpawnZone")

players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local level = Instance.new("IntValue")
	level.Name = "Time"
	level.Parent = leaderstats
	
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		humanoid.Died:Connect(function()
			print(player.Name .. " has died!")
			level.Value = 0
		end)
		
		while task.wait(1) do
			local playersInZone = {}
			for _, child in ipairs(character:GetChildren()) do
				if child:IsA("BasePart") then
					local limbTouchingParts = child:GetTouchingParts()
					if table.find(limbTouchingParts, spawnZone) then
						if not table.find(playersInZone, player) then
							table.insert(playersInZone, player)
						end
					end
				end
			end
			if table.find(playersInZone, player) then
				level.Value += 1
			end
		end
	end)
	
	local data = nil
	
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(player.UserId.."-time")
	end)

	if success then
		level.Value = (data or 0)
	else
		warn("There was an error when attempting to load your data.\n"..errormessage)
	end
end)

players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-level", player.leaderstats.Level.Value)
	end)

	if success then
		print("Player Data has been saved!")
	else
		warn("There was an error while trying to save data.\n"..errormessage)
	end
end)

I’ve used Instance:GetTouchingParts() on each limb of the character, made some minor other changes as well.

1 Like

Hey! Sorry for the late response. This script works perfectly except for, when you die, you will always get Time no matter where you are in the map after you die.

Place this before your player added event:

function shouldAwardTime(player)
	--if player is dead, do not give them levels
	local char = player.Character 
	if not char then return false end 
	local root = char:FindFirstChild("HumanoidRootPart")
	if not root then return false end 
	
	--if for some reason, spawn doesn't exist, reward them
	local CoreSpawn = workspace:WaitForChild("CoreSpawn", 2)
	if not CoreSpawn then warn("Core spawn wasn't found") return true end 
	local SpawnZone = workspace:WaitForChild("SpawnZone", 2)
	if not SpawnZone then warn("Spawn zone wasn't found") return true end  

	--although if they are alive and spawn exists
	local parts = workspace:GetPartsInPart(SpawnZone) 
	--if their humanoid root part is inside the spawn zone:
	if table.find(parts, root) then 
		return false --do not give them points
	else 
		--give them points
		return true
	end
end 

and replace your loop with the following one:

while task.wait(1) do
	if shouldAwardTime(player) then 
		level.Value += 1
	end 
end

Would you mind elaborating further? I’m sorry, I’m still new to scripting.

This is the entire script:

local DatastoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local myDataStore = DatastoreService:GetDataStore("myDataStore")

function shouldAwardTime(player)
	--if player is dead, do not give them levels
	local char = player.Character 
	if not char then return false end 
	local root = char:FindFirstChild("HumanoidRootPart")
	if not root then return false end 
	
	--if for some reason, spawn doesn't exist, reward them
	local CoreSpawn = workspace:WaitForChild("CoreSpawn", 2)
	if not CoreSpawn then warn("Core spawn wasn't found") return true end 
	local SpawnZone = workspace:WaitForChild("SpawnZone", 2)
	if not SpawnZone then warn("Spawn zone wasn't found") return true end  

	--although if they are alive and spawn exists
	local parts = workspace:GetPartsInPart(SpawnZone) 
	--if their humanoid root part is inside the spawn zone:
	if table.find(parts, root) then 
		return false --do not give them points
	else 
		--give them points
		return true
	end
end 

Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local level = Instance.new("IntValue")
	level.Name = "Time"
	level.Parent = leaderstats

	local data = nil

	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(player.UserId.."-time")
	end)

	if success then
		level.Value = (data or 0)
	else
		warn("There was an error when attempting to load your data.\n"..errormessage)
	end

	while task.wait(1) do
		if shouldAwardTime(player) then 
			level.Value += 1
		end 
	end
end)

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		--waits for a maximum of 5 seconds for the character to be added
		--that way you avoid an infinite yield breaking your script
		local Humanoid = character:WaitForChild("Humanoid", 5)
		if not Humanoid then return end 
		
		Humanoid.Died:Connect(function()
			print(player.Name .. " has died!")
			player.leaderstats.Time.Value = 0
		end)
	end)
end)

Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-level", player.leaderstats.Level.Value)
	end)

	if success then
		print("Player Data has been saved!")
	else
		warn("There was an error while trying to save data.\n"..errormessage)
	end
end)