Scripting isn't looping

Hey - would any of you know why the script doesn’t continue after it completes running through the script one successfully? Would I need to do like while true do…? I’m not too sure, and it’s a bit frustrating,

Video:
Game Issue - YouTube

Code:

-- Variables
local DataStoreService = game:GetService("DataStoreService")
local key = "PlayersWhoHaveDied"
local deathStore = DataStoreService:GetDataStore(key)
local PlayerService = game:GetService("Players")
local wasPurchased
local productid = 1215988684 
local marketplace = game:GetService("MarketplaceService")
local player = game.Players.LocalPlayer

bc = BrickColor.new("Bright green")

game.Players.CharacterAutoLoads = false

-- Events
PlayerService.PlayerAdded:Connect(function(player)

	local id = player.UserId
	local success, val = pcall(function()
		return deathStore:GetAsync(id)

	end)
	print(val)
	if success and val then
		print(os.time() - val)

		if os.time() <= val then

			local kickMessage = os.date("You can play again at %c", val)
			player:Kick(kickMessage)

		else

			print("Ban time ended, unbanning...")
			deathStore:RemoveAsync(id)

		end
	end

	local char = player.Character or player.CharacterAdded:Wait()
	local hum = char:FindFirstChild("Humanoid")

	hum.Died:Connect(function()
		
		marketplace:PromptProductPurchase(player,productid)


		marketplace.PromptProductPurchaseFinished:Connect(function(userId, purchase_id, was_purchased) -- Changed "player" to "userId" so that the script does not get conflicted between the player instance and the player number.

			if was_purchased == true then

				player:LoadCharacter()

			else if was_purchased == false then
					print("Banned")
					
					deathStore:SetAsync(id, os.time() + 86400)
					player:Kick("You've been banned for 24 hours.") -- Kept "player" here because that's the Instance you want to kick.

				end
			end
			
		end)
    end)
end)

That’s because the character is destroyed when the humanoid dies. Then another character is created.

How would I go about doing this?

Try this:

-- Variables
local DataStoreService = game:GetService("DataStoreService")
local key = "PlayersWhoHaveDied"
local deathStore = DataStoreService:GetDataStore(key)
local PlayerService = game:GetService("Players")
local wasPurchased
local productid = 1215988684 
local marketplace = game:GetService("MarketplaceService")
local player = game.Players.LocalPlayer

bc = BrickColor.new("Bright green")

game.Players.CharacterAutoLoads = false

-- Events
PlayerService.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		local hum = char:WaitForChild("Humanoid")
		hum.Died:Connect(function()
			marketplace:PromptProductPurchase(player,productid)
		end)
	end)
	
	local id = player.UserId
	local success, val = pcall(function()
		return deathStore:GetAsync(id)

	end)
	print(val)
	if success and val then
		print(os.time() - val)
		if os.time() <= val then
			local kickMessage = os.date("You can play again at %c", val)
			player:Kick(kickMessage)

		else
			print("Ban time ended, unbanning...")
			deathStore:RemoveAsync(id)
		end
	end
end)

marketplace.PromptProductPurchaseFinished:Connect(function(userId, purchase_id, was_purchased)
	local player = game.Players:GetPlayerByUserId(userId)
	if not player or purchase_id ~= productid then return end
	
	if was_purchased then
		player:LoadCharacter()
	else
		print("Banned")
		deathStore:SetAsync(userId, os.time() + 86400)
		player:Kick("You've been banned for 24 hours.")
	end
end)
1 Like

Remove the, local player = game.Players.LocalPlayer

LocalPlayer can only be called from the client.

1 Like

I <3 U, it fixed my issue - appreciate all your support!!! proooo gamer programmer

Yeah, just to explain you need to move the majority of the script inside a function connected to the CharacterAdded event handler, that way every time the CharacterAdded event is fired the majority of the script is ran again.