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

Adding coins to a leaderstat when touching a brick?

Asked by 5 years ago

I have been experimenting for a while and the closest i can get to is

1function onTouched(part)
2    local leaderstats = Instance.new("Folder")
3    leaderstats.Name = "leaderstats"
4    -- Display an 'IntValue' on leaderboard
5    local Coins = Instance.new("IntValue")
6    Coins.Name = "Coins"
7    Coins.Value = Coins.Value + 200
8    Coins.Parent = leaderstats
9end

Any ideas on what was done wrong?

2 answers

Log in to vote
0
Answered by
pwx 1581 Moderation Voter
5 years ago

You almost got it right! But there is 1-2 problems stopping you from completing your ask, let me help you out.

01local function onTouched(part)
02    if part.Parent:FindFirstChildOf('Humanoid') then -- first check if it's a potential player
03        local player = game.Players:GetPlayerFromCharacter(part.Parent) -- see if its a player
04        if player then -- final check
05 
06            if Player:FindFirstChild('leaderstats') == nil then -- check if player already has leaderstats or not
07            local leaderstats = Instance.new('Folder', player) -- you forgot to parent it
08            leaderstats.Name = 'leaderstats'
09            local Coins = Instance.new('IntValue', leaderstats)
10            Coins.Name = "Coins"
11            Coins.Value = 0 -- default value at start
12            end
13 
14            if player:FindFirstChild('leaderstats') ~= nil then -- check if player now has leaderstats      player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 200 -- give them 200 coins
15 
View all 21 lines...
Ad
Log in to vote
0
Answered by 5 years ago

Though ABritishChap's answer is correct, theres a way to make it simpler.

Heres how:

01local function onTouched(part)
02    local plr = game.Players:GetPlayerFromCharacter(part.Parent)
03    if plr then -- If its a player
04        if not plr:FindFirstChild("leaderstat") then -- Check if player already has leaderstats or not
05            local leaderstats = Instance.new("Folder", plr) -- Create leaderstats then parent it to the player
06            leaderstats.Name = "leaderstats"
07            local Coins = Instance.new("IntValue", leaderstats)
08            Coins.Name = "Coins"
09            Coins.Value = 0 -- Default value at start
10        end
11        if plr:FindFirstChild("leaderstats") ~= nil then -- Check if player now has leaderstats     
12            plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value + 200 -- Give them 200 coins
13        end
14    end
15end

Answer this question