I need help when touch part it adds plus 1 to gui

local part = script.Parent
local playersService = game:GetService(“Players”)

part.Touched:Connect(function(partTouched)

if partTouched.Parent:FindFirstChild("Humanoid") then
	
	if playersService:GetPlayerFromCharacter(partTouched.Parent) then
		
		-- when the part is touched script here
		
	end
	
end

end)

I need it so when touched a part my leaderstats go up 1 via a gui
image

1 Like

Something like this if you are have another script to create leaderstats in the player. If not you can do the same thing just create a object to store the value in each player

local player = playersService:GetPlayerFromCharacter(partTouched.Parent)
if player then
  -- when the part is touched script here
  player.leaderstats.Coins.Value += 1
end

--In your client gui script
player.leaderstats.Coins.Changed:Connect(function(value)
  yourTextLabel.Text = value
end)
1 Like

I dont really want leaderstats im more focused on getting the gui to work. Would i pu that scrip in server script service along with the other script? Also the object i want the user is “CoinGiver” as a name so where would i change it so it would work.?

if partTouched.Parent:FindFirstChild("Humanoid") then
        local player = playersService:GetPlayerFromCharacter(partTouched.Parent)
	if player then
		player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1
                script.Parent:Destroy()
	end
end

Do i put that instead of the other scripts. Would i put that script in server script service along with the other script? Also the object i want the user is “CoinGiver” as a name so where would i change it so it would work.?

you cannot make a touched script in server script service if there are multiples of the same part. you need to put the script on the part



Doesnt work

Any ideas you have or no? Its quite hard lol

If you want touched script in server script service I recommend making a folder to hold all the CoinGivers so you can find them easy

For index, child in pairs(workspace.CoinGivers:GetChildren()) do
 child.Touched:Connect ...
end

Also in this script you could create the values to hold the player’s collected number

Game.Players.PlayerAdded:Connect(function(player)
  local coinValue = Instance.New("NumberValue")
  coinValue.Name = "Coins"
  coinValue.Parent = player
end)

Then just link the names and locations together

player.Coins.Value += 1

The following code should work
This one is a normal script on the part

local playersService = game:GetService("Players")
script.Parent.Touched:Connect(function(partTouched)
  if partTouched.Parent:FindFirstChild("Humanoid") then
        local player = playersService:GetPlayerFromCharacter(partTouched.Parent)
	if player then
		player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1
                script.Parent:Destroy()
	end
end
end)

this one is inside the coin text as a local script

local player = game.Players.LocalPlayer
player.leaderstats.Coins:GetPropertyChangedSignal("Value"):Connect(function()
    script.Parent.Text = player.leaderstats.Coins.Value
end)

if the script above doesnt work, insert the script in a normal script and change

local player = game.Players.LocalPlayer

to

local player = script:FindFirstAncestorOfClass("Player")

Maybe you could have the part increase an IntValue, which another script would read and change the TextLabel to whatever number the IntValue is.