Help to fix script

help me fix the error I want to create a day chest but I get an error help me fix it I do not know what the problem is here is my script

coins = "Coins"	
amnt = 50000				
timer = 86400
debounce = false		

function onTouch(hit)
if hit.Parent:findFirstChild("Humanoid") ~= nil and debounce == false then
	if game.Players:findFirstChild(hit.Parent.Name) ~= nil then
		local ThisPlayer = game.Players:findFirstChild(hit.Parent.Name)
		ThisPlayer.leaderstats:findFirstChild(coins).Value = 
ThisPlayer.leaderstats:findFirstChild(coins).Value + amnt
		script.Parent.Transparency = 0
		script.Parent.CanCollide = false
		debounce = true
		wait(timer)		
		
		script.Parent.Transparency = 0
		script.Parent.CanCollide = true
		debounce = false
	end
end
end


script.Parent.Touched:connect(onTouch)
2 Likes

its FindFirstChild not findFirstChild bruh i cant use text smilies?

1 Like

the error that always gives when I approach the chest
Workspace.Daily Reward.ShopPart.Script:11: attempt to index nil with ‘Value’

also why are there two ends? sorta weird

also isnt it :Connect instead of :connect

I’m a beginner writing scripts I saw this script on YouTube

oh i mean i could probably make it look a bit nicer

amnt = 50000				

debounce = false		

function onTouch(hit)
if hit.Parent:findFirstChild("Humanoid") ~= nil and debounce == false then
	if game.Players:FindFirstChild(hit.Parent.Name) ~= nil then
		local ThisPlayer = game.Players:findFirstChild(hit.Parent.Name)
		ThisPlayer.leaderstats:findFirstChild("Coins").Value = 
ThisPlayer.leaderstats:FindFirstChild("Coins").Value + amnt
		script.Parent.Transparency = 1
		script.Parent.CanCollide = false
		debounce = true
		wait(86400)		
		
		script.Parent.Transparency = 0
		script.Parent.CanCollide = true
		debounce = false
	end
end



script.Parent.Touched:Connect(onTouch)

12:11:17.152 Workspace.Daily Reward.ShopPart.Script:24: Expected ‘end’ (to close ‘function’ at line 5), got ; did you forget to close ‘then’ at line 7?

try that, i cleaned up some of your unnessecary values and fixed your caps!

could you paste your entire script?

I get this error that I threw to you above

local players = game:GetService("Players")
local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("CoinsValueSaver")
local MarketPlaceService = game:GetService("MarketplaceService")
game.Players.PlayerAdded:Connect(function(player)

local stats = Instance.new("Folder",player)
stats.Name = "leaderstats"

local Title = Instance.new("StringValue",stats)
Title.Name = "Status"
Title.Value = "Innocent"

local Streak = Instance.new("IntValue", stats)
Streak.Name = "Killstreak"
Streak.Value = 0

local Rep = Instance.new("NumberValue", player)
Rep.Name = "Reputation"
Rep.Value = 0

local Class = Instance.new("StringValue", player)
Class.Name = "Class"
Class.Value = "F-Class"

local BestFusion = Instance.new("Folder", player)
BestFusion.Name = "BestFusion"

local bestFusionName = Instance.new("StringValue", BestFusion)
bestFusionName.Name = "FusionName"
bestFusionName.Value = "None"

local bestFusionMult = Instance.new("IntValue", BestFusion)
bestFusionMult.Name = "FusionMulti"
bestFusionMult.Value = 1

local coins = Instance.new("NumberValue", player)
coins.Name = "Coins"
coins.Value = 0
coins.Changed:connect(function()
ds1:SetAsync(player.UserId, coins.Value)
end)

Hello, fellow developer!
The error in your script is that you are missing an end statement for the onTouch function. Here’s the corrected script:



coins = "Coins"
amnt = 50000
timer = 86400
debounce = false

function onTouch(hit)
    if hit.Parent:findFirstChild("Humanoid") ~= nil and debounce == false then
        if game.Players:findFirstChild(hit.Parent.Name) ~= nil then
            local ThisPlayer = game.Players:findFirstChild(hit.Parent.Name)
            ThisPlayer.leaderstats:findFirstChild(coins).Value = ThisPlayer.leaderstats:findFirstChild(coins).Value + amnt
            script.Parent.Transparency = 0
            script.Parent.CanCollide = false
            debounce = true
            wait(timer)

            script.Parent.Transparency = 0
            script.Parent.CanCollide = true
            debounce = false
        end
    end
end
script.Parent.Touched:Connect(onTouch)

I also included a line at the end to connect the onTouch function to the Touched event of the script’s parent part. This will make sure the function is called whenever the part is touched.
I’m sorry if this not going to work, please reply if you have some questions or warnings.

12:14:46.032 Workspace.Daily Reward.ShopPart.Script:10: attempt to index nil with ‘Value’ - Server - Script:10

first off:
that saving method will very quickly reach your datastore limit, try saving/getting on leave + join
second off:
you are going to need to load the players coin data, otherwise it will be reset
third off:
you are going to have to tab everything up, otherwise it wont be included in the function (i think)

you should probably fix the caps as well

I have a save location I’m just creating this script

yeah but you can only save a datastore so many times
if you reach the limit roblox will stop listening to your datastore calls, and thats not very good

and then where to write this script?

local coins = Instance.new("NumberValue", player)
coins.Name = "Coins"
coins.Value = 0
coins.Changed:connect(function()
ds1:SetAsync(player.UserId, coins.Value)
end) 


local players = game:GetService("Players")
local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("CoinsValueSaver")