Stud Counting Script Being Inconsistent in Counting Studs

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

  1. So a good and trusted friend who’s a way better scripter than I am gave me a script that would allow me to count the number of studs walked by a player, and while I trust this friend their script isn’t very consistent in counting the number of studs walked

  2. I’m not exactly sure what the issue is, as the script looks perfectly fine to me and from what I know. I made a game to show how the studs aren’t being counted consistently, and a link will be provided:

  1. I haven’t tried anything because whenever I try to look something up about a stud counting script on the World Wide Web or here it just comes up with results that aren’t even related to what I was trying to search up

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!

-- 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)
--DON'T EDIT UNLESS YOU KNOW WHAT YOU'RE DOING

game.Players.PlayerAdded:Connect(function(plr) --When the player joins
	local leaderstats = Instance.new("Folder", plr) --Create new folder in the player
	leaderstats.Name = "leaderstats" --Folder name is leaderstats
	
	local studs = Instance.new("IntValue", leaderstats) --Sets up the stat in more detail
	studs.Name = "Studs" --Name of the stat
	studs.Value = 0 --Start value of the stat
end)

pretty sure wait() has a minimum wait time of something like 0.03

1 Like

.Friend's Scripts Check

OnTouched is a very poor way to get this kind of information. When a Player touches an item there will be multiple touches counted, and that’s why you use a debounce. I think part of the issue as @PANDASonNOOB said, is that task.wait() is only .03 seconds, and that doesn’t account for lag.

I’d suggest using a Raycast that points straight down from the player’s HumanoidRootPart, just long enough to see if they are walking on the surface (make it a small bit longer, just in case).
If they are walking on the surface, start counting studs.
If they aren’t walking, or are above the surface then not studs are counted.

1 Like

don’t you just type debounce = false to set a debounce?

edit: okay no watching a video it’s a little harder than that, and also I don’t even know how to use a raycast, I know the basics of scripting like local variables and functions and while/do statements and if/then statements

Sure, but that doesn’t solve the problem that a hit is now being detected every .03 seconds, which only keeps it from crashing because the script is always running and taking over your CPU.

Debounces are good for weapon cooldowns, or in scripts where an item like a door opens when you touch it, but not good for your application.

1 Like

There are plenty of tutorials online, in previous posts and in Roblox create.roblox.com documentation about raycasting.

1 Like

Okay honestly as long as you’re okay with it maybe you can design a script that can do all this? Honestly I’m an amateur scripter and have no idea how to write a better stud counter script. I will take a look at roblox’s tutorial for raycasting though.

It’s how I learned the very basics of raycasting.
I’d never used it and needed a way to determine if a lawnmower was on different surfaces so it’d enable ParticleEmitters to emit grass, dust, sparks, or nothing depending on what the lawnmover was or wasn’t on.
It worked out pretty easily, and since Roblox Studio is all about learning to just play around and give it a try.
If you have issues with your script then you can come back to scripting support.

1 Like

Could you at least please send the tutorial for raycasting so I can take a look at it and try to figure it out?

Here is a quick mock up that is fairly accurate. It is a localscript in the StarterPlayerScripts.

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
repeat task.wait(.1) until character.PrimaryPart ~= nil and player:FindFirstChild("leaderstats")
local prevPos = character.PrimaryPart.Position

while true do
	local a = task.wait(.5)
	player.leaderstats.Studs.Value = player.leaderstats.Studs.Value + (character.PrimaryPart.Position - prevPos).Magnitude
	prevPos = character.PrimaryPart.Position
end

It would also include jumping as well so if you wanted to remove that you’d need to zero out the Y value for the Positions. Also you’d need to fire an event to update the servers values for each player.

Honestly I have no idea what local scripts even do, nor do I know how to remove the Y value. I might be able to do the event part, but honestly I don’t know what to use if onTouch/Touched is a bad choice

Raycasting, where all the Roblox tutorials are.

1 Like

I tried looking at it and have no idea how it works, and the roblox-written tutorial is no help, it’s too vague

So after seeing this video: Advanced Roblox Scripting Tutorial #24 - Raycasting (Beginner to Pro 2019) - YouTube I’m starting to understand this whole raycasting ordeal a little, but not much. Purely for testing purposes, I’m trying to get it to change the color of a part based on the part’s name. I’ll keep playing with it and hopefully eventually be able to rewrite a stud counting script.

1 Like

local scripts run locally, as in, on the client rather then on the server (say little timmy players “free admins” on roblox, and free admins have a local script which allows little timmy to fly. This script would be located and ran on little timmy’s device rather then the server. Meanwhile, say “free admins” have a server script which allows all the players to run commands. This would run on the roblox servers and not on little timmy’s device)

1 Like