Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make it so when standing near a part it gives money every minute?

Asked by 4 years ago

I currently have this script but the problem with it is, it only prints so far. I want it to actually give money. And another problem is it counts it to fast.

local Players = game:GetService("Players")
local ThisPart = script.Parent --make sure the parent is a part
function Check()
    for i, player in pairs(Players:GetPlayers()) do
        local Character = player.Character or player.CharacterAdded:Wait()
        local RootP = Character:WaitForChild("HumanoidRootPart")
        local Dis = (ThisPart.Position-RootP.Position).magnitude --Checks the part/region block's position (you can change "ThisPart" to whatever name you want)
    if Dis <= 10 then --if player is close then they get money!
    print("Player is near!")
    end
end
end
game:GetService("RunService").Heartbeat:Connect(function()
    Check()
end)

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Well first of all it depends how you want it to give you. I suggest you should add a leaderboard.

Once you do simply add your vars and then add the amount of your money in. In terms of adding money fast. I suggest you add a debounce. Here is an example of what I mean

local var = 'Without Debounce'

while true do
    print(var)
end

The output of that will print so much that it may add a execution error.

With a debounce

local var = 'With Debounce'
local debounce = false

while true do
    if debounce == false then
        print(var)
        debounce = true
        wait(1)
        debounce = false
    end
end

This will output With Debounce every 1 second.

Leaderboard- To make a leaderboard you will need to make a script in ServerScriptService

Here is an example leaderboard here-

local plrs = game:GetService('Players')

local money = script.Parent -- Update Money

local function leaderboardSetup(plr)
    local LB = Instance.new('Folder')
    LB.Name = "leaderstats"
    LB.Parent = plr

    local money = Instance.new("IntValue")
    money.Name = 'Money'
    money.Value = 0
    money.Parent = LB
end

local function onTouch(hit)
    local char = hit.Parent
    local humanoid = char:FindFirstChildWhichIsA("Humanoid")
    if humanoid then
        -- Update the plrs leaderboard stat
        local plr = plrs:GetPlayerFromCharacter(parent)
        local LB = player.leaderstats
        local moneyStat = LB and LB:FindFirstChild('money')
        if moneyStat then
            moneyStat.Value = moneyStat.Value + 10 --Change here
        end
    end
end

script.Parent.Touched:Connect(onTouch) --Change
0
I have a leaderboard with it named "Money". So how would I make it give money? (Sorry i'm rather new at scripting) Asherharding10 12 — 4y
0
No problem - Lemme just update the answer CRAZYQUACKY84 113 — 4y
0
There u go CRAZYQUACKY84 113 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

It counts so fast because you are using RunService's "Heatbeat" event, which fires 40 times a second (as opposed to the "RenderStepped" event which fires 60 times a second). This is great for visual and other fast updates, maybe even clocks, but not for something like this, I'd assume.

Switch your checking loop to a simple while statement.

while wait(5) do -- Change 5 to the interval, in seconds.
    -- Something here, like your code, or you can just call your Check() function.
end

10 studs is also not a lot, if that's what you mean by 'it only prints so far', you can experiment with the

if Dis <= 10 then

to change the distance, the part you want is the number, change the 10 to whatever, higher number means higher distance.

I can't exactly give you advice on 'giving money', as I'm not sure what you're referring to.

Answer this question