When touching Model (Part) It should add plus 1 to gui (Not working)

The coin when touching should add +1
A local script in the text label
Script inside the local script.
Script in server script service

1 Like

Change the Touched script to:

for _, v in pairs(script.Parent:GetDescendants()) do
  if v:IsA("BasePart") then
    v.Touched:Connect(function(hit)
      -- Script
    end
  end
end

So are you talking about the coins script? Or the 2nd photo when im actually touching it and adding plus 1

The first one, inside the Coins model

1 Like

It still unfortunately hasnt worked, any ideas?

1 Like

i was initially thinking it has something to do with the text label?

Can you unfold the two models inside ‘Coins’?

image
Lol i know, a lot of parts.

1 Like

The :Connect() wasn’t closed properly. Add a closing bracket right after the 2nd end

Which of the models is the actual coin? Could you show what the ‘Coins’ model looks like?

image
Doesnt work TestyLike3

image

Pardon me, I meant the first end.

image
Ive still got 0 via the GUI

Okay, I’m just going to take a guess that the ‘Coins’ model is the actual coin itself, try this!

local PS = game:GetService("Players")

local coin = script.Parent

for _,v: BasePart in ipairs(coin:GetDescendants()) do
	if v:IsA("BasePart") then
		v.Touched:Connect(function(hit)
			local model = hit:FindFirstAncestorWhichIsA("Model")
			local player = PS:GetPlayerFromCharacter(model)
			local coins = player and player.PlayerStats:FindFirstChild("Coins")
			
			if player and coins then
				coins.Value += 1
				script:Destroy()
			else
				warn("uh oh")
			end
		end)
	end
end
1 Like


As you can see the “uh oh” bit is actually multiplying although it isnt going up in the gui. My question is, is there a problem with the text label, should there be a text button?

1 Like

That’s not good, it means the script cannot find either the player or ‘player.PlayerStats.Coins’. Try printing both ‘player’ and ‘coins’.

Try this instead

local coin = script.Parent

for _,v: BasePart in ipairs(coin:GetDescendants()) do
	if v:IsA("BasePart") then
		v.Touched:Connect(function(hit)
		   local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			
			if player then
				Player.PlayerStarts.Coins.Value += 1
				script:Destroy()
			else
				warn("uh oh")
			end
		end)
	end
end
1 Like

While just relying on ‘hit.Parent’ works most of the time, there will be cases where an accessory will be detected and not one of the body parts.

I personally use

hit:FindFirstAncestorWhichIsA("Model")

to get the character model, regardless on whether the touched part was a body part or not.