Point System One-Time | Need Help

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)
2 Likes

ocal?
I think you meant local.
Can I have a screenshot of your output and Explorer?

1 Like

When I copied my script I didn’t copy the L, My output is empty. The Data is at ServerScriptService and my level script is at game.Workspace.LVL1. They are both regular scripts. Do you know how I can make it give a point only one time? So if you exit the game and rejoin. It won’t give you another point. Thanks!

if alreadycollected = true then
-- If you have touched the door, and already collecte is true, points = points (No gained points)
end)

Now, make a datastore to save alreadycollected.

Basically, heres a script:

local alreadycollect = false

door.Touched:Connect(function()
alreadycollect = true
end)

if alreadycollect == true then
door.Touched:Connect(function()
points = points + 0
end)
end

So, in this script, if you have already got it, and the door is touched, you get no points.
Now, make a datastore so it saves alreadycollect to see if they touched it, left and rejoined.
Hope this helps :slight_smile:

1 Like

You could condense this down because you only need to have one .Touched connection.

local canCollect = true

door.Touched:Connect(function(hit)
  if canCollect then
    canCollect = false
    points += 0 --This is a nifty function that Roblox finally added in so you can add a variable to itself without having to reference it.
  end
end)

A debounce would also be another good thing so that it doesn’t register multiple touches.

@MrLonely1221
Yes, use a debounce so it does not register 2+ touches on the door, it may glitch it and give it 2 points.
Couldn’t you just do
coins = coins
instead of points +=0?
Like, I have 5 coins, I touch it, so my coins stay the same.
@Wrvel
Use a debounce, like I and @MrLonely1221 said above.
Add hit to the function (Sorry for not saying that before)
You can use points = points + 0 (Gives no points), points = points (Stays the same, does the same thing) or you can use points +=0 (It still works ok, but I just use points = points + 0.

I just sent the same code you had before.

And points += X is just shorthand for points = points + X