Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
G_han 3
8 years ago

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)

0
Edited to use code block. BlueTaslem 18071 — 8y

2 answers

Log in to vote
0
Answered by 8 years ago

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
Ad
Log in to vote
0
Answered by 8 years ago

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)

Answer this question