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)
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
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.