Untitled

 avatar
unknown
plain_text
6 months ago
1.6 kB
3
Indexable
local ds = game:GetService("DataStoreService")
local plrstatsds = ds:GetDataStore("PlayerStatsDS")
local cooldown = false

-- the configuration made in script itself
local itemName = "Construction Paper Turkey Headband" -- replace this with your item name
local cooldownTime = 5 -- the cooldown for players to get item
local remoteEvent = game.ReplicatedStorage:WaitForChild("GiveItemEvent")

script.Parent.Touched:Connect(function(hit)
	if not cooldown then
		if hit.Parent:FindFirstChild("Humanoid") and game.Players:FindFirstChild(hit.Parent.Name) then
			cooldown = true
			local plr = game.Players:FindFirstChild(hit.Parent.Name)
			print("Player touched: "..plr.Name)

			-- gets the player from datastore
			local beforedata = plrstatsds:GetAsync("Player_"..plr.UserId)
			if beforedata then
				local datachange = beforedata
				local items = {}

				local beforeitems = datachange.PlayerData.Inventory or {}

				-- this will check if player already had item
				if not table.find(beforeitems, itemName) then
					-- this will add item to player inventory
					for i, v in pairs(beforeitems) do
						table.insert(items, v)
					end
					table.insert(items, itemName)
					datachange.PlayerData.Inventory = items


					plrstatsds:SetAsync("Player_"..plr.UserId, datachange)
					print("Data set! Gave item: "..itemName)


					remoteEvent:FireClient(plr, itemName)
				else
					warn("Player already has the item!")
				end
			else
				warn("Player data not found!")
			end


			wait(cooldownTime)
			cooldown = false
		end
	end
end)
Editor is loading...
Leave a Comment