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

How do I add points to someone when they touch a part?

Asked by 4 years ago
Edited 4 years ago

This is my leaderboard code and I am trying to figure out a way to have points given to someone who is touching a part.

local DataStoreService = game:GetService("DataStoreService")

local DataStore = DataStoreService:GetDataStore("MoneyStats")

game.Players.PlayerAdded:Connect(function(Player)

local Leaderstats = Instance.new("Folder")

Leaderstats.Name = "leaderstats"

Leaderstats.Parent = Player

local Points = Instance.new("IntValue")

Points.Name = "Points"

Points.Value = DataStore:GetAsync(Player.UserId) or "0"

Points.Parent = Leaderstats

end)

game.Players.PlayerRemoving:Connect(function(Player)

DataStore:SetAsync(Player.UserId, Player.leaderstats.Points.Value)

end)

0
You have something at the end of Leaderstats.Parent = Player. Just want to make sure that that's not a part of the Script. AntiWorldliness 868 — 4y
0
Yeah. it's not. MrBaconFTW 2 — 4y

1 answer

Log in to vote
0
Answered by
TriteA1 30
4 years ago
Edited 4 years ago

Hi, MrbaconFTW

To start off with your leaderstats script is broken. I have fixed it:

local Name = "Points"
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("MoneyStats")

game.Players.PlayerAdded:Connect(function(Player)

    local Leaderstats = Instance.new("Folder")
    Leaderstats.Name = "leaderstats"
    Leaderstats.Parent = Player

    local Value = Instance.new("IntValue")
    Value.Name = Name
    Value.Parent = Leaderstats

end)

game.Players.PlayerRemoving:Connect(function(Player)
    DataStore:SetAsync(Player.UserId, Player.leaderstats[Name].Value)

end)

As you can see I have changed the name of the IntValue on lines 11, 12 and 13 to value instead of "Points".

Now adding points when a player hits a part is actually quite simple. Insert this into the part:

debounce = false --Debounce makes it so the "Touched" function can't be spammed.

script.Parent.Touched:Connect(function(hit) 
    if not debounce then debounce = true
           local player = game.Players:GetPlayerFromCharacter(hit.Parent);--Getting Player.
           If player then
               player.leaderstats.Points.Value = player.leaderstats.Points.Value + 1 --Adding a number to "Points". 
           end
           wait(1) --The cooldown before the player can get another point. (Debounce)
           debounce = false 

    end
end)

Hopefully this helps.

Ad

Answer this question