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

Script still adds to the leaderboard value even though a condition isnt met?

I have made a script to make this orb only add 1 to your stats one time. It works if I do not tweak the specified value in orb folder, however if I attempt to change the value manually in test mode, it still awards an orb collected. The orbfolder has a datastore to it, but it doesnt seem to be the problem as I tested it in studio changing the value myself with the orb still being awarded. If anyone could identify what is wrong, I would appreciate it. My code is below:

game.Players.PlayerAdded:Connect(function(join)
script.Parent.Touched:Connect(function(plr)
	local humanoidcheck = plr.Parent:FindFirstChild("Humanoid")
	if humanoidcheck ~= nil then
		local orbfolder = join.orbfolder
		if orbfolder.orb1.Value == 0 then
		local orbs = join.leaderstats.Orbs
		orbs.Value = orbs.Value + 1
		orbfolder.orb1.Value = 1
	end
	end
	end)
end)

Thank you!

4 comments

0

Delete the PlayerAdded event. See if that helps

0

It will break the entire script as I need that to find the player's orbfolder.

0

He is correct, remove the PlayerAdded event and use game.Player:GetPlayerFromCharacter(plr.Parent) to get the player instead

0

game.Players*

1 answer

game.Players.PlayerAdded is unneeded here. At the moment your script will create a new touched event every time a Player joins, and hence will run the code every time it is touched for each Player.

What you want to do is to use :GetPlayerFromCharacter(char) to get the player instead:

script.Parent.Touched:Connect(function(part)
	local humanoid = part.Parent:FindFirstChild('Humanoid')
	if (humanoid) then
		local player = game.Players:GetPlayerFromCharacter(humanoid.Parent);
		if (player) then
			-- Run your code
		end
	end
end)

5 comments

0

Alright, thank you! Will the way you wrote the part to check for humanoid and player work?

0

Yes, when using object references in an 'if' statement, it will always return true if the object exists.

0

So this still doesnt fix the problem where it still adds even when the condition for orb1 is false

0

Your code looks correct on that front. It should only add to the value if join.orbfolder.orb1.Value is equal to 0. Maybe check your hierarchy is correct?

0

I believe its correct, there is no error in the console

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