I am currently working on a farming game and am trying to set up different fields. Im trying to make it where you purchase each field at a door (Similar to Pet Simulator). I have the current script that works but I cant figure out how to make it save the purchase and deduct “Rice” from the players leaderstats.
Door = script.Parent
local debounce = false
function getPlayer(humanoid)
local players = game.Players:children()
for i = 1, #players do
if players[i].Character.Humanoid == humanoid then return players[i] end
end
return nil
end
function onTouched(hit)
if debounce == false then
local human = hit.Parent:findFirstChild("Humanoid")
if (human == nil) then return end
local player = getPlayer(human)
debounce = true
if (player == nil) then return end
local stats = player:findFirstChild("leaderstats")
local sp = stats:findFirstChild("Rice")
if sp == nil then return false end
if (sp.Value >= 10) then
sp.Value = sp.Value - 10
Door.Transparency = .5
Door.CanCollide = false
wait(3)
Door.CanCollide = true
Door.Transparency = 0
debounce = false
end
end
end
connection = Door.Touched:connect(onTouched)
How does one do this?
Can you please post the code in a code block
like this
The image is a bit hard to read the code from,
thanks!
All fixed. Sorry bout that. 
1 Like
Wouldn’t this be something you would do with CollectionService? CollectionService | Documentation - Roblox Creator Hub. Then you could tag each door and run the script for each door. If you have not seen collection service before here is a tutorial: https://www.youtube.com/watch?v=-oPE0PL2Zew. Two things that you should try:
1. local stats = player:WaitForChild:("leaderstats")
2. FYI, you are missing an indent for the stats line, and Door.CanCollide has an indent for some reason.
Finally, your current error may be that it is Instance:FindFirstChild, with the first F capitalized. https://www.youtube.com/watch?v=-oPE0PL2Zew
Hope this helps!
Someperson576
1 Like
You can use a bool value to determine if the player can go through the door and save that with a datastore:
Here is a video for more information:
For deducting rice, you can save an IntValue
or NumberValue
with the same datastore and change the value in a (server)script like so:
local rice = plr.leaderstats:WaitForChild("Rice") -- define plr as the player
-- code here
rice.Value = rice.Value - 100 -- change 100 to price you want to deduct
As a heads up please make sure you indent your script to make it more professional and easier to read, Thanks!
I followed the tutorial and was able to do a CollectCervice but when the script was made it didnt work.
(Im not very much of a scripter more of a builder
)
This is the code what was put into the scrip for the ServerScriptService:
local CollectionService = game:GetService("CollectionService")
local TaggedParts = CollectionService:GetTagged("Rice")
for _, TaggedPart in pairs (TaggedParts) do
local give = 1
local debounce = false
script.Parent.Touched:connect(function(part)
if not debounce then
debounce = true
if part.Parent:findFirstChild("Humanoid") then
local player = game:GetService("Players"):GetPlayerFromCharacter(part.Parent)
if player then
local leaderstats = player:findFirstChild("leaderstats")
if leaderstats then
local money = leaderstats:findFirstChild("Rice")
money.Value = money.Value + give
end
end
end
wait(.5)
debounce = false
end
end)
Dont know whats up 
Ok, two more things:
Please indent your code, it makes it much easier to read.
Also, it is local leaderstats = player:WaitForChild("leaderstats")
or it is local leaderstats = player:FindFirstChild("leaderstats")
Capitalization and spelling is key.
Also, make sure you tagged the doors with “Rice”
1 Like