Fellow Scripters,
I need your help as soon as possible.
I’m working on a game where if you join a group and enter a through the door and touch the part it will give you a point. But I have data save on. I don’t want people to keep closing and entering getting it over and over. So I want to make a onetime point system when you touch a part, even if you exit the game and rejoin. It won’t give you another point.
I’m using this code
local level = script.Parent
local function level_complete(otherPart)
local player = game.Players:FindFirstChild(otherPart.Parent.Name)
if player then
local completed_folder = player.completed
local alreadyCompleted = completed_folder:FindFirstChild(level.Name)
if not alreadyCompleted then
local level_name = Instance.new('StringValue')
level_name.Name = level.Name
level_name.Parent = completed_folder
player.leaderstats.Tickets.Value = player.leaderstats.Tickets.Value + 1
end
end
end
level.Touched:Connect(level_complete)
The Data Store script I used is
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")
local function onPlayerJoin(player) -- Runs when players join
local leaderstats = Instance.new("Folder") --Sets up leaderstats folder
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local tickets = Instance.new("IntValue") --Sets up value for leaderstats
tickets.Name = "Tickets"
tickets.Parent = leaderstats
local completed = Instance.new('Folder')
completed.Name = 'completed'
completed.Parent = player
local playerUserId = "Player_" .. player.UserId --Gets player ID
local data = playerData:GetAsync(playerUserId) --Checks if player has stored data
if data then
-- Data exists for this player
tickets.Value = data
else
-- Data store is working, but no current data for this player
tickets.Value = 0
end
end
local function onPlayerExit(player) --Runs when players exit
local success, err = pcall(function()
local playerUserId = "Player_" .. player.UserId
playerData:SetAsync(playerUserId, player.leaderstats.Tickets.Value) --Saves player data
end)
if not success then
warn('Could not save data!')
end
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)