Why isn't my button working?

I’m making a button simulator game and I put this script in a part.

script.Parent.Touched:Connect(function(hit)
	local player = hit.Parent.Name
	local gameplayer = game.Players[player]
	if gameplayer.leaderstats.Clicks.Value >= 50 then
		gameplayer.leaderstats.Clicks.Value = gameplayer.leaderstats.Clicks.Value - 50
		gameplayer.leaderstats.Multiplier.Value = gameplayer.leaderstats.Multiplier.Value + 1
	end
end)

But, it isn’t changing any stats.

1 Like

Are there any errors being logged? Try this code

script.Parent.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if plr then
		local leaderstats = plr:findFirstChild('leaderstats')
		if not leaderstats then print('no leaderstats') return end
		local Clicks = leaderstats:findFirstChild('Clicks')
		if not Clicks then print('no clicks val') return end
		local Multiplier = leaderstats:findFirstChild('Multiplier') 
		if not Multiplier then print('no multiplier') return end
		
		if Clicks.Value >= 50 then
			Clicks.Value = Clicks.Value - 50
			Multiplier.Value = Multiplier.Value + 1
		end
	end
end)
1 Like

no errors were being logged before and after i tried your code. pretty odd. if it matters, the parent of what needs to be touched is a model.

Workspace
	Buttons (Folder)
		Model (Primary Part = Buy)
			Buy (Part)
				Script
				Part (button rim)

Put prints in your script to see if the button is firing when it’s touched and what the values of the variables are.

for example

script.Parent.Touched:Connect(function(hit)
    print("button touched ")
	local player = hit.Parent.Name
    print("by ",Name)
	local gameplayer = game.Players[player]  
        print(gameplayer.leaderstats.Clicks.Value)
	if gameplayer.leaderstats.Clicks.Value >= 50 then 
		gameplayer.leaderstats.Clicks.Value = gameplayer.leaderstats.Clicks.Value - 50
		gameplayer.leaderstats.Multiplier.Value = gameplayer.leaderstats.Multiplier.Value + 1
	end
end)

It prints
“button touched
by TailsSlashMiles92
1”
So it seems it’s not getting the value and it’s staying at 1.

local player = hit.Parent.Name
local gameplayer = game.Players[player]

maybe change gameplayer to
local gameplayer = game.Players:GetPlayerFromCharacter(hit.Parent)

not sure if this will change much*

it just has the same result, as GetPlayerFromCharacter() does the exact thing as game.Players[hit.Parent] - gets the name and searches it in players

i know that
idk why your script isnt working, is it local?

It’s a regular script in Workspace. If we can’t figure this problem out, I’m just gonna do clickDetection which is really inefficient for a Button Simulator.

i just tested your script, it is working in my game, are you sure you spelt the leaderstats correctly?

is CanTouch true?
image

leaderstats was spelled correctly, and canTouch is true. I’m not 100% sure what’s going on.

can we get a screenshot of the workspace?

Alright, here


Also i meant to say it isnt in a model it’s in it’s part.
Also heres another script that i think i should tell you about.

1 Like

(im talking about the first image and if its a model and its in a part it wont change anyhting)
that doesn’t change anything, i really don’t understand whats going on here, thats confusing

Not sure why. You wanted a screenshot of workspace so i gave it to you…

OH, THAT CHANGES EVERYTHING, since its a local script, the leaderstat value doesn’t change in the actual server.

1 Like

oh. i guess i really didnt think that one through. Should i change it to a script and alter the code?

yeah, put it in severscriptservice

and just make it
game.Players.PlayerAdded:Connect(function(plr)

nvm delete the variable , i just reread the code

also you can do
while wait(0.1) do

end

It’s finally working, thanks!

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = player:WaitForChild("leaderstats")
	local plrClick = leaderstats:WaitForChild("Clicks")
	local plrMulti = leaderstats:WaitForChild("Multiplier")

	while wait(0.1) do
		plrClick.Value += (1 + (1*plrMulti.Value))
	end
end)