Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Upon touching the cookie, why does it not give a point?

Asked by 3 years ago
Edited by JesseSong 3 years ago

Please encode Lua code in the Lua block code tag (look for the Lua icon in the editor).

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.

01local DataStoreService = game:GetService("DataStoreService")
02local HasCookie1 = DataStoreService:GetDataStore("HasCookie1")
03amnt = 1 -- amount of cookie
04function onTouched(part)
05local h = part.Parent:findFirstChild("Humanoid")
06if (h~=nil) then   
07local thisplr = game.Players:findFirstChild(h.Parent.Name)     
08local player = game.Players:GetPlayerFromCharacter(h.Parent)       
09local playerKey = "id_" .. player.UserId       
10if (thisplr~=nil) then         
11local stats = thisplr:findFirstChild("leaderstats")        
12if (stats~=nil) then               
13local score = stats:findFirstChild("Cookies")              
14if (score~=nil) and DataStoreService:HasCookie1(player.UserId, false) then                 
15score.Value = score.Value + amnt                   
View all 23 lines...
0
Please use a code block around code MightBeAHaxxer 24 — 3y
0
Why have you defined the player twice? is there a reason? MightBeAHaxxer 24 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

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!

01local DataStoreService = game:GetService("DataStoreService")
02local HasCookie1 = DataStoreService:GetDataStore("HasCookie1")
03 
04cookiesToGive = 1 -- amount of cookie
05 
06local function giveCookies(hit)
07    local char = hit.Parent
08    local humanoid = char:FindFirstChildWhichIsA("Humanoid")
09 
10    if humanoid then
11        local player = game.Players:GetPlayerFromCharacter(char)
12 
13        if player then
14            local hasCookie = HasCookie1:GetAsync("id_" .. player.UserId)
15            if hasCookie == false then
View all 32 lines...
Ad

Answer this question