I want to get the name of the player who touches the cash stack and give him cash
gives me this error when someone touches the cash stack
couldn’t find anything similar to this
local Players = game:FindService("Players")
script.Parent.Touched:connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") ~= nil then
local pl = hit.Parent.Name
Players.pl.Cash += 100
wait()
script.Parent:Destroy()
end
end)
why not just make a leaderstats script and reference the players chase from there? it’s much easier and beginner-friendly
put a server script into server script service and use this code: (if you already have leaderstats ignore this)
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player
local cash = Instance.new(“IntValue”)
cash.Name = “Cash”
cash.Value = 0
cash.Parent = leaderstats
end)
then put this script in your part:
local Player = game:GetService("Players")
script.Parent.Touched:Connect(function(hit) --- Object that hits the part
local PlayerCharacter = Player:GetPlayerFromCharacter(hit.Parent) -- Get the player that hits the part
if PlayerCharacter then
local Leaderstats = PlayerCharacter:WaitForChild("leaderstats")
--- Reward goes here.
Leaderstats.Cash.Value = Leaderstats.Cash.Value + --value
end
end)
end)
if you need any help setting it up give me a reply.
Here you are indexing the Players service instead of getting the actual player. You can use the GetPlayerFromCharacter function to get the player that has Touched the part.
local Players = game:FindService("Players")
script.Parent.Touched:connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") ~= nil then
if hit.Parent.Humanoid.Health > 0 then
local pl = Players:GetPlayerFromCharacter(hit.Parent)
pl:WaitForChild("leaderstats").Cash.Value += 100
wait()
script.Parent:Destroy()
end
end
end)
local Players = game:GetService("Players")
script.Parent.Touched:connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") ~= nil then
if hit.Parent.Humanoid.Health > 0 then
local pl = Players:GetPlayerFromCharacter(hit.Parent)
wait(2)
local stats = pl:WaitForChild("leaderstats").Cash
stats.Value = stats.Value + 100
wait()
script.Parent:Destroy()
end
end
end)