Hello all, so I made a script which is “Money” and when u touch its gives you money for touching it but literally anything can touch it in the game and it will delete
Source Code
script.Parent.Touched:Connect(function(plr)
local name = plr.Parent.Name
local playerInstance = game:GetService("Players")
local player = playerInstance:WaitForChild(name)
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 5
end)
the problem is that this is touching things other than players and you should be using :GetPlayerFromCharacter to see if it is a player.
script.Parent.Touched:Connect(function(hit)
local plr = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
if plr then
plr.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 5
end
end)
script.Parent.Touched:Connect(function(part)
if part.Parent:FindFirstChild("Humanoid") and game:GetService("Players"):FindFirstChild(part.Parent.Name) then
local name = plr.Parent.Name
local playerInstance = game:GetService("Players")
local player = playerInstance:WaitForChild(name)
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 5
end
end)