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

Points given to everyone, but meant for only one player?

My script is made to award a win to whoever touches the brick, but it awards points equal to the number of players in the server, and awards everyone at the same time. Here is the script.

local brick = script.Parent
function onTouched(hit)
    for i,player in ipairs(game.Players:GetPlayers()) do
        if player.Character then
            local stat = player:FindFirstChild("leaderstats")
            if stat then
                player.leaderstats.Wins.Value = player.leaderstats.Wins.Value +1
            end
        end
    end
end
brick.Touched:connect(onTouched)

1 comment

0

Edited to use code block.

2 answers

✓ Accepted answer

The problem you were having was that you were getting all the players in the server and giving everyone the points. I can see why you did this, to get the player, but their is an easier way to get the Player who touched the brick. To do this, we use :GetPlayerFromCharacter() The character is the part that touches the brick, and so this would get the Player

local brick = script.Parent

function onTouched(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		local stat = player:WaitForChild("leaderstats")
		if stat then
			player.leaderstats.Wins.Value = player.leaderstats.Wins.Value + 1
		end
	end
end

brick.Touched:connect(onTouched)

As you can see in the script, I got the Player using :GetPlayerFromCharacter()and inside the () I put hit.Parent, which hit is the Character and Parent is the Characters parent, which is the Player

Another problem you may or may not face, is that the Wins value may not be created yet, so to fix this we just add a :WaitForChild()

local brick = script.Parent

function onTouched(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		local stat = player:WaitForChild("leaderstats")
		if stat then
			local wins = stat:WaitForChild("Wins")
			if wins then
				wins.Value = wins.Value + 1
			end
		end
	end
end

brick.Touched:connect(onTouched)

This is just an extra pracaution, but it may not need to be used, but I would suggest using it, as some people might have slower connection, meaning that they may touch this brick before the Win value is created.

Hope this helped and be sure to +1 and accept answer.

  • NinjoOnline

Ok, it's really hard to tell what you have scripted because first it's not in a Code Block and 2nd it's all jumbled up and doesn't look pretty but, from what I read it seemed like the script is inside of the brick that the player is touching and u want the player that touches that brick to get +1 in wins value... And that's about all the information I need to make a script that could help you out. The following script is a server script(normal script) put inside of the brick.

local brick = script.Parent
brick.Touched:connect(function(hit)
	if debounce then return end
	debounce = true
	local humanoid = hit.Parent:findFirstChild("Humanoid")
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if humanoid and player then
		player.leaderstats.Wins.Value = player.leaderstats.Wins.Value +1
		wait(5)
	end
	debounce = false
end)

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