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

How exactly do I add to a value?

Asked by 5 years ago

I'm trying to make a voting mechanic for my game, but when I touch the part that counts the vote, nothing is added to the value and it stays as 0. (This is only a fragment of the code and the local is at the top of the script)

local Button = script.Parent

Button.Touched:Connect(function()
    Button.VoteCount.Value = Button.VoteCount.Value + 1
end)

Is the something wrong with the touch function? Before anyone asks, I did try FindFirstChild and this is a server script.

0
Where is the Button located DanzLua 2879 — 5y
0
game.Workspace.VotingCentre.Buttons.Button1 SindexMon 6 — 5y

3 answers

Log in to vote
0
Answered by 5 years ago

I believe it isn't working because the game is FE. You could use remote events to get this to work.

-- LocalScript in Starter Player scripts

local Button = game.Workspace.VotingCentre.Buttons.Button1

Button.Touched:Connect(function()
    game.ReplicatedStorage.RemoteEvent:FireServer()
end)

-- Server  Script in SererScriptService

game.ReplicatedStrorage.RemoteEvent.OnServerEvent:Connect(function()
    Button.VoteCount.Value = Button.VoteCount.Value + 1
end)
0
That isn't working either SindexMon 6 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

I fixed it by repositioning the Touched event in the script oof

Log in to vote
0
Answered by
OnaKat 444 Moderation Voter
5 years ago
Edited 5 years ago

You need to check that the hit is player

local Button = script.Parent

Button.Touched:Connect(function(hit) --You can get the part that touch here
    if game.Players:GetPlayerFromCharacter(hit.Parent) then
        Button.VoteCount.Value = Button.VoteCount.Value + 1
    end
end)

But this will spam the vote count. So try add something to check if the player that hit hasn't been vote before.

I try to add folder.

local Button = script.Parent

Button.Touched:Connect(function(hit) --You can get the part that touch here
    if game.Players:GetPlayerFromCharacter(hit.Parent) then
        local plr =  game.Players:GetPlayerFromCharacter(hit.Parent)
    if plr.Character:FindFirstChild("VoteCheck") then
    else
            Button.VoteCount.Value = Button.VoteCount.Value + 1
        local check = Instance.new("Folder",plr.Character)
        check.Name = "VoteCheck"
        end
    end
end)

Answer this question