Friend's Scripts Check

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Simply put, I want to make a stud counter in my game. I found a friend who made a game with a stud counter, but the scripts they gave me don’t seem to work. Please note that I am not angry at the friend, even pro devs make mistakes in the code sometimes
  2. What is the issue? Include screenshots / videos if possible!
    I know pretty much nothing about scripting, I can read the script and it makes a little sense to me, but not much
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve tried asking my friend about it but they couldn’t do much to help. I have also tried searching yt and here up and down for any kind of stud counter, but it’s never what I want and/or doesnt work

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

Just to be safe, I will be keeping my friend fully anonymous

-- How this works: The player must walk on the part for the studs to be counted in. Eg: If the speed is 30 make the wait time (0.030) --
local part = script.Parent
local canGet = true

local function onTouch(otherPart)
    local humanoid = otherPart.Parent:FindFirstChild('Humanoid')
    if humanoid then
        local player = game.Players:FindFirstChild(otherPart.Parent.Name)
        if player and canGet then
            canGet = false
			player.leaderstats.Studs.Value = player.leaderstats.Studs.Value + 1 
            wait(0.016)
            canGet = true
        end
    end
end

part.Touched:Connect(onTouch)
--[[Savin'
		Dem
			Stats	
--]]
game.Players.PlayerRemoving:connect(function(player)
	local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Stats")

local statstorage = player:FindFirstChild("leaderstats"):GetChildren()
for i =  1, #statstorage do
	datastore:SetAsync(statstorage[i].Name, statstorage[i].Value)
	print("saved data number "..i)
	
end
print("Stats successfully saved")	
end)


--[[
	Loadin'
		Dem
			Stats
--]]
game.Players.PlayerAdded:connect(function(player)
		local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Stats")
	
	player:WaitForChild("leaderstats")
	wait(1)
	local stats = player:FindFirstChild("leaderstats"):GetChildren()
	for i = 1, #stats do			
	stats[i].Value = datastore:GetAsync(stats[i].Name)
	print("stat numba "..i.." has been found")
		end
end)

Please note that I also want the stud counter to reset once the player leaves the game, so the second one might not even be needed
Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

Shouldn’t this be local player = game.Players:GetPlayerFromCharacter(otherPart.Parent). I’m not sure.

  1. The wait(0.016) line should be inside the if player and canGet then block. This is because the wait function will pause the script execution, and the canGet variable will not be set to true until after the wait function completes.
  2. The player.leaderstats.Studs.Value line should be inside the if player and canGet then block as well.
  3. The player:WaitForChild("leaderstats") line should be inside the game.Players.PlayerAdded event handler. This is because WaitForChild will pause the script execution until the specified child is found, and you want to wait until the player has fully joined the game before trying to access their leaderstats.
  4. The wait(1) line should be removed. There’s no need to wait for the player’s leaderstats to load, as the WaitForChild function will handle this for you.
  5. The print statements should be removed or modified to include more context about what’s happening in the script.

Here’s how the script should look after these changes:

local part = script.Parent
local canGet = true

local function onTouch(otherPart)
    local humanoid = otherPart.Parent:FindFirstChild('Humanoid')
    if humanoid then
        local player = game.Players:FindFirstChild(otherPart.Parent.Name)
        if player and canGet then
            canGet = false
            player.leaderstats.Studs.Value = player.leaderstats.Studs.Value + 1
            wait(0.016)
            canGet = true
        end
    end
end

part.Touched:Connect(onTouch)

game.Players.PlayerRemoving:Connect(function(player)
    local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Stats")

    local statstorage = player:FindFirstChild("leaderstats"):GetChildren()
    for i = 1, #statstorage do
        datastore:SetAsync(statstorage[i].Name, statstorage[i].Value)
    end
end)

game.Players.PlayerAdded:Connect(function(player)
    local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Stats")

    player:WaitForChild("leaderstats")
    local stats = player:FindFirstChild("leaderstats"):GetChildren()
    for i = 1, #stats do
        stats[i].Value = datastore:GetAsync(stats[i].Name)
    end
end)

I tried the code. Seems to be working fine. Maybe if you can tell me more in detail I can do something. Are you sure you added the leaderstats?

Does not matter, it should work either way.

super sorry for getting back so late (so late that there’s a blue box thingy next to the text box as I’m typing this message, and I’ll probably get a warning or something for “necroposting” even though it’s my post), but I don’t even know what leaderstats is, I’m very new to scripting and know the bare minimum, like locals, if/then and while/do statements, just learned functions…

1 Like