Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Leaderboard changing brick not working?

i want to make a part that can change the value of a leaderstat and set it to a certain value without adding to it but for instead it just sets it to the value i want it to change to when the brick is touched

game.Players.PlayerAdded:connect(function(player)
	script.Parent.Touched:connect(function()
		local plrstats = player.leaderstats.stage
		plrstats.Value = 1
	end)
end)

i am not sure what is wrong with it here. and i tried this as well

game.Players.PlayerAdded:connect(function(player)
	script.Parent.Touched:connect(function()
		local plrstats = player.leaderstats.stage
		plrstats.Value = plrstats.value = 1
	end)
end)

but that didnt work either

2 comments

0

You don't need to use PlayerAdded in this. You can get whatever part touched it by putting a variable in the parameters since it passes it. So just check if that is a player and increase their stage

0

its useless putting .Touched when the player joins, use plrstats.Value = plrstats.Value + 1

1 answer

There are quite a few issues with your code here.

When it comes to the BasePart.Touched signal, it'll fire without collision context; you must verify that a Character Rig initiated contact with the Part. You can still derive the respectful Player Object with the the GetPlayerFromCharacter method of Players.

With greatneil's statement, the approach you have taken with handling the a .Touched event is quite unorthodox, and will lead to some complications. It's recommended you work with the part-of-contact parameter provided to the callback when the signal is fired.

Lastly, you're using the assignment operator, when you should be applying the shorthand addition-assignment operator:

local Players = game:GetService("Players")


local Part = script.Parent


local function OnTouched(CharacterBodyPart)
	local Player = Players:GetPlayerFromCharacter(CharacterBodyPart.Parent)
	if (Player) then
		---------------
		Player.leaderstats.Coins.Value += 1
	end
end


Part.Touched:Connect(OnTouched)

####Lowercase connect is deprecated, use Connect.

This discussion is preserved as it was. New answers, comments, and votes are closed.