Instance.Name cannot be used to change location

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
image

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)
1 Like

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.


gives me this error

do you have a leaderstats script?

i have the leaderstats script ,

it just gives me this error when i try to do it in a script (not local) and i want it to get destroyed after giving cash

Players.pl.Cash += 100

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.

is the leaderstats folder in the player or is it giving the player the leaderstats script?

i already changed the code to

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)

but it cant find leaderstats

In reference to this, below cash.Parent = leaderstats

type:
leaderstats.Parent = player

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)


same thing

have you done this yet? You would do this in a new Script.

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)

forgot the leaderstats.Parent = player part, thanks

1 Like

oh my bad. i was writing the scripts on my phone and completely forgot the parent part. happy i could help though!

Edit your original post with that fix implemented (in case others view this thread).

1 Like