Need help with coin/clicks bug

local plr = game:GetService(“Players”)

script.Parent.Touched:Connect(function(obj)
if obj.Name == “HumanoidRootPart” then
local n = obj.Parent.Name
local localp = plr:FindFirstChild(n)
local Clicks = localp.leaderstats.Clicks
Clicks.Value += 10
script.Enabled = false
script.Parent.Transparency = 1
wait(30)
script.Parent.Transparency = 0
script.Disabled = false
end
end)

some reason keeps setting my coin/clicks to 10 how do i get it so it doesnt reset the number

2 Likes

are there any errors in output?

Instead of using += use

Clicks = Clicks + 10

Why? There is no difference and it wastes time with extra characters.

local plr = game:GetService(“Players”)

script.Parent.Touched:Connect(function(obj)
  if obj.Parent:FindFirstChild(“HumanoidRootPart”) then
    local n = obj.Parent.Name
    local localp = plr:WaitForChild(n)
    local Clicks = localp.leaderstats.Clicks
    Clicks.Value += 10
    script.Enabled = false
    script.Parent.Transparency = 1
    wait(30)
    script.Parent.Transparency = 0
    script.Disabled = false
  end
end)

it still resets to the lower number

local player = game:GetService(“Players”)

script.Parent.Touched:Connect(function(obj)
  if obj.Parent:FindFirstChild(“HumanoidRootPart”) then
    local n = obj.Parent.Name
    local localp = player:WaitForChild(n)

    local Clicks = localp.leaderstats.Clicks
    Clicks.Value = Clicks.Value + 10
   
        script.Enabled = false
        script.Parent.Transparency = 1

        wait(30)

        script.Parent.Transparency = 0
        script.Disabled = false
    end
end)

This should work.

1 Like

How exactly is it resetting? How are you handling this?

But I would like to point out something here:

Just use :GetPlayerFromCharacter(), it would make this a lot easier, and should make this look better


local p = Players:GetPlayerFromCharacter(obj.Parent)
--[[
    when a Touched event is activated, it will give you the part that touched 
    (referred to as 'hit')
    which in the case of a Player, it could be a leg, or an Arm
    hit.Parent will give you the Potential Model of the Player if they do touch
    the part, which is where :GetPlayerFromCharacter() comes in
    what this function does is give you the player based on their Character
    pretty useful in most cases, but it isnt always perfect, if a player isn't
    found, it will return nil, and would cause errors, so secure it with a statement
    to check if the Player Actually exists before using it, like the example below
--]]


if p then -- if p, will fire if its a truthy value (not false or nil)
    local clicks = p.leaderstats.Clicks
    clicks.Value += 10

--// rest of the code

like say i touch the coin it resets my money back to 10 if i have more than 10 moneys

Make sure you dont have any other scripts interfering/resetting your coins value. There doesent seem to be anything wrong in the script itself you showed

okay ill check thank you so much for your help!