Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make it so you gain money while standing in a specific area?

I have script that creates a folder name's it leaderstats and a value named gold. I want it so when you stand in a certain area it'll add 40 gold every minute.

This is all I got to but i'm clueless and not good at all at scripting


local Gold = game.Players.leaderstats.Gold

function onTouched(hit)
	while true do
	wait(60)
	Gold.Value = Gold.Value + 40
end
end

5 comments

0

You've got options with this. You can either use region3 or magnitude. If you wanted you just use touched event with a touch ended event, but it is very sensitive, and would most likely fail

0

What's the difference between region3 and magnitude?

0

Magnitude checks if your character is in a certain range of a brick, and region3 checks if you are inside of a designated region that you set up via script. For magnitude you would have to have a part and check if the character is close enough to that part to get points

0

I would want to use magnitude then, but how would I utilize this in my script?

0

Anyone at all that can help please?

1 answer

Using magnitude, here's how it'll look:

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!")
			local Gold = player.leaderstats.Gold --Money is being given to player
			Gold.Value = Gold.Value + 1
    end
end
end
game:GetService("RunService").Heartbeat:Connect(function()
    Check()
end)

6 comments

0

Thank's alot, but would this be a normal script or a local and where would i put it?

0

Sorry for the late reply, this code would be in a server script located in the part where you want players to get gold from. If you want to modify the distance just change 10 where it says "if Dis <= 10 " to what ever number you want.

0

Alright so, I have the part script with code in it. But when i go up to it doesn't print anything, it also give's me this warning "Unknow global region (PartName)"

0

I updated the code, it should print now.

0

Thanks! it prints how would i make it give a currency tho?

0

It should give money/gold now but I highly recommend you practice or search more about magnitude and getting player in a server script.

This discussion is preserved as it was. New answers, comments, and votes are closed.