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

Error while Increasing leaderstats?

I want to increase my star count. However, it triggers my debug message. Leaderstats Constructor, global script in Workspace:

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	if game.Players:FindFirstChild("leaderstats")then
		print("Succses")
	end
	local Stars = Instance.new("IntValue", leaderstats)
	Stars.Name = "Stars"
end)

Global Script inside the star:

script.Parent.Part.Touched:Connect(function()
	if game.Players:FindFirstChild("leaderstats") then
		game.Players.LocalPlayer.leaderstats.Stars.Value = game.Players.LocalPlayer.leaderstats.Stars.Value + 1
	else
		print("uhh")
	end
end)
script.Parent.Wedge1.Touched:Connect(function()
	if game.Players:FindFirstChild("leaderstats") then
		game.Players.LocalPlayer.leaderstats.Stars.Value = game.Players.LocalPlayer.leaderstats.Stars.Value + 1
	else
		print("uhh")
	end
end)
script.Parent.Wedge2.Touched:Connect(function()
	if game.Players:FindFirstChild("leaderstats") then
		game.Players.LocalPlayer.leaderstats.Stars.Value = game.Players.LocalPlayer.leaderstats.Stars.Value + 1
	else
		print("uhh")
	end
end)
script.Parent.Wedge3.Touched:Connect(function()
	if game.Players:FindFirstChild("leaderstats") then
		game.Players.LocalPlayer.leaderstats.Stars.Value = game.Players.LocalPlayer.leaderstats.Stars.Value + 1
	else
		print("uhh")
	end
end)
script.Parent.Wedge4.Touched:Connect(function()
	if game.Players:FindFirstChild("leaderstats") then
		game.Players.LocalPlayer.leaderstats.Stars.Value = game.Players.LocalPlayer.leaderstats.Stars.Value + 1
	else
		print("uhh")
	end
end)
script.Parent.Wedge5.Touched:Connect(function()
	if game.Players:FindFirstChild("leaderstats") then
		game.Players.LocalPlayer.leaderstats.Stars.Value = game.Players.LocalPlayer.leaderstats.Stars.Value + 1
	else
		print("uhh")
	end
end)

1 answer

✓ Accepted answer

You have to use the :GetPlayerFromCharacter() function. Also, use the built-in hit parameter.

script.Parent.Part.Touched:Connect(function(hit)
	local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)

	if player then
		if player:FindFirstChild("leaderstats") then
			player.leaderstats.Stars.Value = (player.leaderstats.Stars.Value + 1)
		end
	else
        	print("uhh")
	end
end)

Line 2 Gets the player. Line 4 checks if the part that the part touched is a player. Line 5 checks if the player has leaderstats. Line 6 adds the stars to the player.

2 comments

0

thanks mate

0

no problem

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