Bug with my zone door script!

I’ve managed to make a zone door script with RemoteEvents but the problem is when i try to take awaya coins from the player when they buy the door


This bug appears when you buy it
image
i think its because i had no debounce because i was doing it by hit.Parent but db didnt work then itryed changing the doors position after they bought so that they can’t hit anymore!

local db = false
script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
          local Buy = game.ReplicatedStorage.Events.Buy
         if db == false then
            db = true
             player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - 1250
				Buy:FireClient(player:GetPlayerFromCharacter(hit.Parent))
			 wait(.2)
            db = false
        end
    end
end)

Buy basically just makes the door invisible and cancollide set to false,i woudnt think it would give me a negative number

1 Like

There are multiple issues with the script.
Firstly, making the part invisible and cannot collide will still fire the .Touched event. I suggest completely deleting it from the LocalScript. Second, you should add a logic gate to check if a player has enough cash to unlock the zone or not. It prevents a player’s cash from going negative. Lastly, just because a model has a humanoid in it, it does not mean that it’s a player. You should check if the Player variable is valid or not.

2 Likes
local db = false
script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
          local Buy = game.ReplicatedStorage.Events.Buy
         if db == false then
            db = true
			if player and player.leaderstats.Coins.Value >= 1250 then
				 player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - 1250
				Buy:FireClient(player:GetPlayerFromCharacter(hit.Parent))
			 wait(.2)
            db = false
			end
        end
    end
end)

i tryed what you said :slight_smile:

heres the local script for the events

local yourRemote = game.ReplicatedStorage.Events.Buy
yourRemote.OnClientEvent:Connect(function()	
local Player = game.Players.LocalPlayer
if Player.leaderstats.Coins.Value >= 1250 then
       game.Workspace.Snow:Destroy()
  end	
end)

local yourRemote2 = game.ReplicatedStorage.Events.Buy2
yourRemote2.OnClientEvent:Connect(function()
	local Player = game.Players.LocalPlayer
	if Player.leaderstats.Coins.Value >= 1250 then
		game.Workspace.D:Destroy()
	end
end)


local yourRemote3 = game.ReplicatedStorage.Events.Buy3
yourRemote3.OnClientEvent:Connect(function()
	local Player = game.Players.LocalPlayer
	if Player.leaderstats.Coins.Value >= 1250 then
		game.Workspace.Snow1:Destroy()
	end
end)


1 Like
local db = false
script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
          local Buy = game.ReplicatedStorage.Events.Buy
         if db == false then
            db = true
			if player.leaderstats.Coins.Value >= 1250 then
				 player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - 1250
				Buy:FireClient(player:GetPlayerFromCharacter(hit.Parent))
			 wait(.2)
            db = false
			end
        end
    end
end)

dosent seem to work!

1 Like

ServerScript line 10,you’re using :GetPlayerFromCharacher() on a player instance.

oh ok i think i should jsut put player right their

should iput hit.Parent or player in the brackets?

Put player inside of the bracket. (line 10)

1 Like

ok so when i play the game my leaderstats are gone

Well, most likely other scripts are causing this problem.

1 Like

Heres my whole leaderstats script

--//leadersats & Group Benefits\\--
local GroupId = 5032042
local Reward = 500

starting_money = 0

starting_money2 = 0

starting_money3 = 0

function onPlayerEntered(player)
	wait(1) --delay for loading time
	local leaderstats = Instance.new("IntValue")
	leaderstats.Parent = player
	leaderstats.Name = "leaderstats"
	local Coins = Instance.new("IntValue")
	Coins.Parent = leaderstats
	Coins.Name = "Coins"
	Coins.Value = starting_money
	
	local BC = Instance.new("IntValue")
	BC.Parent = leaderstats
	BC.Name = "Bread Collected"
	BC.Value = starting_money3
	
	local Rebirths = Instance.new("IntValue")
	Rebirths.Parent = leaderstats
	Rebirths.Name = "Rebirths"
	Rebirths.Value = starting_money2
	
	if player:IsInGroup(GroupId) then
		Coins.Value = Coins.Value +Reward
	end
end



--//MarketPlaceService\\--
local MPS = game:GetService("MarketplaceService")

MPS.ProcessReceipt = function(receiptInfo)
	if receiptInfo.ProductId == 686369007 then  
		local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
		player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1000
		return Enum.ProductPurchaseDecision.PurchaseGranted
		
	elseif receiptInfo.ProductId == 686369345 then
		local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
		player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 5000
		return Enum.ProductPurchaseDecision.PurchaseGranted
		
		elseif receiptInfo.ProductId == 686369689 then 
		local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
		player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 10000
		return Enum.ProductPurchaseDecision.PurchaseGranted
		
		elseif receiptInfo.ProductId == 686372686 then 
		local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
		player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 50000
		return Enum.ProductPurchaseDecision.PurchaseGranted
		
		elseif receiptInfo.ProductId == 686373270 then 
		local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
		player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 100000
		return Enum.ProductPurchaseDecision.PurchaseGranted
		
		elseif receiptInfo.ProductId == 686374019 then
		local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
		player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1000000
		return Enum.ProductPurchaseDecision.PurchaseGranted
		
		elseif receiptInfo.ProductId == 6863747509 then 
		local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
		player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 5000000
		return Enum.ProductPurchaseDecision.PurchaseGranted
		
		elseif receiptInfo.ProductId == 686412335 then 
		local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
		player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 125555555555555555
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
end
--//Gamepasses\\--
--VIP
local id = 7047605

game:GetService("MarketplaceService").PromptGamePassPurchaseFinished:Connect(function(plr,ido,purchased)
	if purchased and ido == id then
--code
	end
end)

--Infinite Storage
local id = 7047608

game:GetService("MarketplaceService").PromptGamePassPurchaseFinished:Connect(function(plr,ido,purchased)
	if purchased and ido == id then
--code
	end
end)

--250+ Storage
local id = 7047611

game:GetService("MarketplaceService").PromptGamePassPurchaseFinished:Connect(function(plr,ido,purchased)
	if purchased and ido == id then
--code
	end
end)

--500+ Storage
local id = 7047614

game:GetService("MarketplaceService").PromptGamePassPurchaseFinished:Connect(function(plr,ido,purchased)
	if purchased and ido == id then
--code
	end
end)
--2x Coins
local id = 7047617

game:GetService("MarketplaceService").PromptGamePassPurchaseFinished:Connect(function(plr,ido,purchased)
	if purchased and ido == id then
--code
	end
end)

You don’t have a listener for your player entered function.

local db = false
script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
          local Buy = game.ReplicatedStorage.Events.Buy
         if db == false then
            db = true
			if player.leaderstats.Coins.Value >= 1250 then
				 player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - 1250
				Buy:FireClient(player)
			 wait(.2)
            db = false
			end
        end
    end
end)
``` this is still not working :confused:

Heres the local script for th events

local yourRemote = game.ReplicatedStorage.Events.Buy
yourRemote.OnClientEvent:Connect(function()	
local Player = game.Players.LocalPlayer
if Player.leaderstats.Coins.Value >= 1250 then
       game.Workspace.Snow:Destroy()
  end	
end)

local yourRemote2 = game.ReplicatedStorage.Events.Buy2
yourRemote2.OnClientEvent:Connect(function()
	local Player = game.Players.LocalPlayer
	if Player.leaderstats.Coins.Value >= 1250 then
		game.Workspace.DesertDoor:Destroy()
	end
end)


local yourRemote3 = game.ReplicatedStorage.Events.Buy3
yourRemote3.OnClientEvent:Connect(function()
	local Player = game.Players.LocalPlayer
	if Player.leaderstats.Coins.Value >= 5000 then
		game.Workspace.Snow1:Destroy()
	end
end)



You need a listener for onPlayerEntered function in the leaderboard script.

1 Like

ik just addded that but,this script wont work

local db = false
script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
          local Buy = game.ReplicatedStorage.Events.Buy
         if db == false then
            db = true
			if player.leaderstats.Coins.Value >= 1250 then
				 player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - 1250
				Buy:FireClient(player)
			 wait(.2)
            db = false
			end
        end
    end
end)

Is there any error output? And try putting prints after each logic gate so we can narrow down the problem.

1 Like

i did this,and it did not print Hi

local db = false
script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
          local Buy = game.ReplicatedStorage.Events.Buy
         if db == false then
            db = true
			if player.leaderstats.Coins.Value >= 1250 then
				 player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - 1250
				Buy:FireClient(player)
				print("Hi")
			 wait(.2)
            db = false
			end
        end
    end
end)

Try putting one right at the beginning, one after the first if, one after the debounce checker and one right before the last end.

Make sure that the prints output different strings.

1 Like
local db = false
script.Parent.Touched:Connect(function(hit)
	  print("HI")
    if hit.Parent:FindFirstChild("Humanoid") then
	    print("HI")
          local player = game.Players:GetPlayerFromCharacter(hit.Parent)
          local Buy = game.ReplicatedStorage.Events.Buy
         if db == false then
            db = true
            print("HI")
			if player.leaderstats.Coins.Value >= 1250 then
				 player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - 1250
				Buy:FireClient(player)
				print("Hi")
			 wait(.2)
            db = false
			end
        end
    end
end)
print("HI")
1 Like