Issues with datasaving

Ello!
I am making a game that there will some events but the datasaving will not delete itself
( So the player will lose all progress of the game )
May anyone help me?

local PlayerData = DataStoreService:GetDataStore("GhostEventData") -- Used for initial check
local CC = Enum.ChatColor.Red

game.Players.PlayerAdded:Connect(function(player)
	-- Load player's data from the DataStore
	local success, hasSeenGhost = pcall(function()
		return PlayerData:GetAsync(player.UserId) -- Load data (no default value here)
	end)

	-- Error handling for loading data
	if not success then
		warn("Failed to load data for player " .. player.Name .. ": " .. hasSeenGhost)
		return
	end

	-- If data exists, the player has seen the ghost event before
	if hasSeenGhost then
		-- Change lighting to night time
		game.Lighting.TimeOfDay = "02:00:00"

		wait(0.1) -- Short delay before stopping sound

		-- Stop background town music if it exists
		local townMusic = workspace.Folder:FindFirstChild("Town")
		if townMusic and townMusic:IsA("Sound") then
			townMusic:Stop()
		end

		-- Spawn ghost rig
		local rig = game.ReplicatedStorage:FindFirstChild("Rig2")
		if rig then
			rig = rig:Clone()
			rig.Parent = workspace

			-- Chat function for the ghost
			local function Chat(message)
				game:GetService("Chat"):Chat(rig.Head, message, CC)
			end

			wait(12)
			Chat("WHY?")
			wait(1)
			Chat("ARE YOU STILL HERE")
			wait(2)

			-- Instead of saving to "GhostEventData" before teleport, skip it or use a different DataStore
			-- Commenting out the original save to "GhostEventData"
			--[[
			pcall(function()
				PlayerData:SetAsync(player.UserId, true) -- This would save to "GhostEventData"
			end)
			--]]

			-- Teleport the player to another game
			local placeId = 110199345976845 -- Replace with the correct place ID for the target game
			game:GetService("TeleportService"):Teleport(placeId, player) -- Teleport the player
			Chat("Better run from that")
			PlayerData:GetAsync(nil)

			-- Remove the sign if it exists
			local sign = workspace:FindFirstChild("Sign")
			if sign then
				sign:Destroy()
			end
		end
	end
end)

Sorry big script too
Script is on server script
thanks !

2 Likes
PlayerData:GetAsync(nil)

Is this supposed to reset their data? If so, did you pay any mind to the function name being “Get” and not “Set”…?

1 Like

Sorry I kind of suck at scripting I have’nt scripted and this is my first time using data save :frowning:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.