As the title says, when I touch the cookie it does not give a point. If you're wondering what the purpose of the "HasCookie1" datastore, it is to prevent people from rejoining to pick up the cookie again & again awarding more points.
local DataStoreService = game:GetService("DataStoreService") local HasCookie1 = DataStoreService:GetDataStore("HasCookie1") amnt = 1 -- amount of cookie function onTouched(part) local h = part.Parent:findFirstChild("Humanoid") if (h~=nil) then local thisplr = game.Players:findFirstChild(h.Parent.Name) local player = game.Players:GetPlayerFromCharacter(h.Parent) local playerKey = "id_" .. player.UserId if (thisplr~=nil) then local stats = thisplr:findFirstChild("leaderstats") if (stats~=nil) then local score = stats:findFirstChild("Cookies") if (score~=nil) and DataStoreService:HasCookie1(player.UserId, false) then score.Value = score.Value + amnt HasCookie1:SetAsync(playerKey, true) end end end script.Parent:remove() end end script.Parent.Touched:Connect(onTouched)
hello fellow scripter! I heard u needed help; i fixed it for you - and made it a lot more stable. your old code used a global function; which are not stable - i recommend using a local function more often. anyways - here's your fixed code!
local DataStoreService = game:GetService("DataStoreService") local HasCookie1 = DataStoreService:GetDataStore("HasCookie1") cookiesToGive = 1 -- amount of cookie local function giveCookies(hit) local char = hit.Parent local humanoid = char:FindFirstChildWhichIsA("Humanoid") if humanoid then local player = game.Players:GetPlayerFromCharacter(char) if player then local hasCookie = HasCookie1:GetAsync("id_" .. player.UserId) if hasCookie == false then local leaderstats = player:WaitForChild("leaderstats") local cookies = leaderstats:FindFirstChild("Cookies") if cookies then cookies.Value = cookies.Value + cookiesToGive HasCookie1:SetAsync("id_" .. player.UserId, true) end else warn(player.Name .. " already has the cookie.") end end end end script.Parent.Touched:Connect(function(hit) giveCookies(hit) end)