When i touch a part i want it to add plus 1 to my gui not working?

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)

image
image
image)

1 Like

It appears to me that at the end of your server script it destroys it’s parent, which is named ‘coins’ as showed in the output. If I’m incorrect please inform me.

Edit: Wrote this in a rush, it appears I am incorrect. Unfortunately a fix cannot be found because there is not enough context shown quite yet in this topic. Could we take a look at the leaderstats instance inside of the player?

Yes i pretty sure thats correct. Im quite new to scripting so im sorry if im not great.

The error message is saying that leaderstats is an IntValue. Leaderstats should be a folder with an intvalue named Coins inside of it. Can I see the script where you add the player’s leaderstats?

I have just looked in workspace i type up leaderstats and all i see is this
image
Just a heads up this wasnt by me, i had another 1 but its was wrong so this guy said this one would work, so i have just gone from here to be fair.

Check if the leaderstats instance is created by a script

and which scripts should i check???

If you press ctrl+F in studio there will be a pop up menu to find lines of code in scripts, what I would recommend to find there is instance.new function

Oh is that that the output your talking about?

My first post was incorrect and it seems that the server script destroying it’s parent is not the issue here. The output screenshot you posted at the beginning is telling me that there is an instance named ‘leaderstats’ inside of game.Players.LocalPlayer, and the screenshots you posted shows the server and local scripts inside of a ScreenGui’s descendants, which, if the ScreenGui is inside of StarterGui, would be directly inside of game.Players.LocalPlayer.PlayerGui once the game runs.

So what do you want me to send a picture of?

Run the game and take a screenshot of what game.Players.planets0001.leaderstats.Intvalue contains

Do you have any script (usually a server script) which creates your stats’ folder & the coins IntValue object?

That’s what I tried to figure out earlier, didn’t really get an answer though

17:17:08.389 Infinite yield possible on ‘Players.planets0001.PlayerGui.ScreenGui:WaitForChild(“OpenButton”)’ - Studio
I cannot open this

Im not sure i think thats what we are trying to figure out.

Yeah, usually you create a leaderstats folder for each player each time when they join the server

Open it through the explorer tab in studio, not through console commands

ah ok this is what come up
image

Use a debounce on the Touched event.

local players = game:GetService("Players")
local debounce = false
local part = script.Parent

part.Touched:Connect(function(hit)
	if debounce ~= true then
		debounce = true
		local character = hit.Parent
		local humanoid = character:FindFirstChildWhichIsA("Humanoid")
		if typeof(humanoid) == 'Instance' and humanoid:IsA("Humanoid") and humanoid.Health <= 0 then
			return
		end
		local player
		if typeof(character) == 'Instance' then
			player = players:GetPlayerFromCharacter(character)
		end
		if typeof(player) == 'Instance' and player:IsA("Player") then
			(player:WaitForChild("leaderstats"):WaitForChild("Coins") :: IntValue).Value += 1 -- luau be like 'InVaLiD tYpE' and so i went 'hush'
			part:Destroy()
		end
	end
end)

Another edit: The reason your code isn’t working is because Coins isn’t a valid member of leaderstats, make sure you use WaitForChild if that isn’t the case. The game may still need time to load.