So, I am trying to make a Part that awards the player with PlayerPoints and updates some DataStore leaderstats also when it's touched. Everything is working fine, but when I insert the part of the code that's supposed to change the leaderstats, it breaks. I took this part from this article on the wiki: api-reference/function/Players/GetPlayerFromCharacter The output gives out no errors too. Leaderstats are stored in a folder inside the player with an IntValue in it.

This is the code: (I added comments indicating where that part starts, and where it ends)

local PointsService = game:GetService("PointsService")
local debounce = false

local Players = game:GetService("Players") -- This line is part of the leaderstat awarder

  

script.Parent.Touched:Connect(function(hit)

if hit.Parent:FindFirstChild("Humanoid") then

if not debounce then

debounce = true

if script.Parent.Name == "DuplicatedLol" then

PointsService:AwardPoints(game.Players[hit.Parent.Name].UserId,75)

local player = Players:GetPlayerFromCharacter(script.Parent) -- This line starts the leaderstat awarder part

if not player then return end

if player then

player.leaderstats.Points.Value = player.leaderstats.Points.Value + 75 -- This line ends the leaderstat award part

wait(0.5)

script.Parent:Destroy()

end

end

end

end

end)

Here's the DataStore script:

local DataStore = game:GetService("DataStoreService")

local ds = DataStore:GetDataStore("PointsSaved")

  

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

local ppstats = Instance.new("Folder", player)

ppstats.Name = "leaderstats"

local Points = Instance.new("IntValue",ppstats)

Points.Name = "Points"

Points.Value = ds:GetAsync(player.UserId) or 0

ds:SetAsync(player.UserId, Points.Value)

Points.Changed:Connect(function()

ds:SetAsync(player.UserId, Points.Value)

end)

end)

  

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

ds:SetAsync(player.UserId, player.leaderstats.Points.Value)

end)

Hopefully I'm not screwing up something easy.

7 comments

1

You're not even using DataStores, so why does the question title say it even though you're not using them?

0

Also, is this a local script or normal script?

0

I am. One sec, I'll edit the post to have the DataStore script. Also, both are ServerScripts.

0

Done.

0

Oh and, they both are separate scripts. The first one is located inside the part that awards the player, and the second one is stored inside ServerScriptService.

1

Change GetPlayerFromCharacter(script.Parent) to GetPlayerFromCharacter(hit.Parent).

0

ah thanks a bunch, that worked