Why does this code give points out insanely fast

local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local Players = game:GetService(“Players”)
local Workspace = game:GetService(“Workspace”)
local Debris = game:GetService(“Debris”)

– Assuming PointsBlock is the part to be picked up and follows the player
local pointsBlock = game.Workspace.PointsBlock
local pickupBlock = Workspace:FindFirstChild(“pickupBLock”)

local leaderStatM = require(ReplicatedStorage.LeaderstatsModule)

– Function to make the pointsBlock follow the player’s head
local function followPlayer(player)
local character = player.Character or player.CharacterAdded:Wait()
local head = character:WaitForChild(“Head”)
local connection

connection = game:GetService("RunService").Heartbeat:Connect(function()
	
		if pointsBlock and head then
			pointsBlock.Position = head.Position + Vector3.new(0, 3, 0)-- Adjust the Vector3 value as needed
						if player:FindFirstChild("leaderstats") then
							local leaderstats = player:FindFirstChild("leaderstats")
								local points = leaderstats:FindFirstChild("Points")
									if points then 
									points.Value = points.Value + 1 
								
						end
				 end
    		else
        connection:Disconnect()
    end
end)

-- Disconnect the follow function if the player dies
player.CharacterRemoving:Connect(function()
    if connection then
        connection:Disconnect()
        pointsBlock.Position = character.Position + Vector3.new(0, 5, 0) -- Adjust the Vector3 value as needed to hover above ground
    end
end)

end

– Function to handle the pickup action
local function onPickup(hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
if player then
followPlayer(player)
end
end

– Connect the pickupBlock Touched event to the onPickup function
pointsBlock.Touched:Connect(onPickup)

– Handle existing players and new players joining
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
– Optional: Implement logic if you want something to happen when a player spawns or respawns
print(“Welcome!”)
end)
end)

for _, player in Players:GetPlayers() do
player.CharacterAdded:Connect(function(character)
– Optional: Implement logic if you want something to happen for already existing players
print(“Dont die!”)
end)
end

– Make sure the pointsBlock is initially positioned above the pickupBlock
if pointsBlock and pickupBlock then
pointsBlock.Position = pickupBlock.Position + Vector3.new(0, 5, 0) – Adjust the Vector3 value as needed
end

Why do my code give out points so fast, I can’t seem to slow it down.

The reason why the points are increasing so quickly is that the code is incrementing the points.Value on every heartbeat, which occurs multiple times per second. The line points.Value = points.Value + 1 inside the RunService.Heartbeat connection is responsible for this rapid point gain.

To slow down the point accumulation, you can introduce a delay or a condition to limit how often the points are incremented. Here are a few suggestions:

  1. Add a delay between point increments:

    • Create a new variable to track the last time points were incremented.
    • Inside the RunService.Heartbeat connection, check if enough time has passed since the last increment before adding a new point.
    • Update the last increment time after adding a point.
  2. Introduce a cooldown period:

    • Create a variable to store the cooldown time (e.g., 1 second).
    • Inside the RunService.Heartbeat connection, check if enough time has passed since the last point increment.
    • If the cooldown period has elapsed, increment the points and reset the cooldown timer.
  3. Increment points based on a specific condition:

    • Instead of incrementing points every heartbeat, define a condition that must be met before incrementing points.
    • For example, you could increment points when the player presses a specific key or performs a certain action.

Here’s an example of how you could implement a cooldown period:

local cooldownTime = 1 -- Cooldown time in seconds
local lastPointIncrement = 0 -- Track the last time points were incremented

connection = game:GetService("RunService").Heartbeat:Connect(function()
    local currentTime = tick() -- Get the current time
    if currentTime - lastPointIncrement >= cooldownTime then -- Check if cooldown period has elapsed
        if player:FindFirstChild("leaderstats") then
            local leaderstats = player:FindFirstChild("leaderstats")
            local points = leaderstats:FindFirstChild("Points")
            if points then
                points.Value = points.Value + 1
                lastPointIncrement = currentTime -- Update the last increment time
            end
        end
    end
    -- Rest of your code...
end)

In this example, the cooldownTime variable determines the minimum time (in seconds) between point increments. The lastPointIncrement variable stores the last time points were incremented. Inside the RunService.Heartbeat connection, the code checks if the current time minus the lastPointIncrement time is greater than or equal to the cooldownTime. If this condition is met, it increments the points and updates the lastPointIncrement time.

Adjust the cooldownTime value to control how often points are incremented. A higher value will slow down the point accumulation rate.

Thank you for this correction, however I’ve already implemented this set of code and it didn’t work. Furthermore, I did figure out my problem and it was the premature disconnection line of code I had; inside of the followPlayer function. Thank you for your help though man, you definitely verified I was in the right path!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.