Problems with datastore and part.Touched

hi,
i found bug inside my scripts sell script and shop script.
is anyone can tell me why is with bugs?
here is the scripts

shop script:

script.Parent.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	local Rps = game.ReplicatedStorage
	if hit.Parent:FindFirstChild("Humanoid") and hit.Parent:FindFirstChild("Humanoid") ~= nil then
		Rps.Events.Pads.ShopPad:FireClient(player)
	end
end)

sell script:

local db = true

script.Parent.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	local Rps = game.ReplicatedStorage
	if hit.Parent:FindFirstChild("Humanoid") and hit.Parent:FindFirstChild("Humanoid") ~= nil and player:WaitForChild("Data").WandsValue.Value >= 1 and db == true then
		player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + player.Data.WandsV.Value
		player.Data.WandsV.Value = 0
		player.Data.WandsValue.Value = 0
		db = false
		wait(.5)
		db = true
	end
end)

here is the bugs:
image
image
thank you for the help.

What seems to be the issue is that you’re defining the local ‘player’ before checking if it’s actually a player. You need to change the placement of your if statement that checks if it has a humanoid to the first thing that the script does, like so:

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		print("A player has touched")
	else
		print("Touched by another object")
	end
end)
1 Like

The error of FireServer in your script you must have the first varbiable has a player so for example:

Event.OnServerEvent:Connect(function(player, variable1, etc)
    print(player)
    print(variable1)
end)

Event:FireServer(myVariable, etc)
1 Like