im trying to make it so when you have touched the part, it will add stats once. When you leave the game and join back and touch the part, it will not add the stats because you already touched the part before. Basically like a datastore but with touching a part and not number values

--//ServerScript
wait(2)

local sell = script.Parent
local debounce = false
local tool = game.ReplicatedStorage.Present
local toFire = script.Parent.Client
local ds = game:GetService("DataStoreService")
local dss = ds:GetDataStore("NotMyRealDataStoreNameBtw")

game.Players.PlayerAdded:Connect(function(plr)
local key = "upgrade-"..plr.UserId
local folder = Instance.new("Folder",plr)
folder.Name = "Upgrades"
local save = dss:GetAsync(key)

if save then
	for i = 1,#save do
		local temp = Instance.new("BoolValue",folder)
		temp.Name = save[1]
	end
end
end)

sell.Touched:Connect(function(hit)
	if not debounce then
	debounce = true
	if hit.Parent:FindFirstChild("Humanoid")then
		local plr = workspace[hit.Parent.Name]
		local player = game.Players:GetPlayerFromCharacter(plr)
		local key = "upgrade-"..player.UserId
		local bool = Instance.new("BoolValue",player.Upgrades)
		bool.Name = "Upgrade"
		bool.Value = true
		local activated = {}
		local Capacity = game.ReplicatedStorage.Capacity
		local upgrades = player:WaitForChild("Upgrades")
		local money = player.leaderstats.Money
		
		if not upgrades:FindFirstChild("Upgrade")and money.Value >= 2000 then
		local reward = 25
		Capacity.Value = Capacity.Value + reward
			for i,v in pairs(player.Upgrades:GetChildren())do
			if v:isA("BoolValue")then
				table.insert(activated, v.Name)
				dss:SetAsync(key,activated)
				print("Upgrade Success")
			else
				print("Upgrade already purchased")
				Capacity.Value = Capacity.Value + 0
		wait(1)
		debounce = false
		
				end
			end
		end
	end
	end
end)

5 comments

0

Why are you getting the player from the workspace

0

Also this code is very inconsistent and why are you calling wait() at the top.

0

i like to use wait in my scripts, I got the player from the workspace so i can get the player from character. Will remove the wait time though

0

But you can use the Character property instead of from the workspace. By getting from the workspace, you run the risk of breaking your scripts if someone sniped a property name, either maliciously or unintentionally, or they name themselves something in your workspace, your code will break

0

I didn't know that. Thanks for the info. Did some changes to it and it works