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

How to make the script work only once per player when he runs the script?

Asked by 5 years ago

I tried to make a script that instancing a BoolValue, and if Value was true, player can't run script on second time. But when i tried to test, it's just wont work. Nothing in Output. Can you help me please?

player = game.Players.PlayerAdded:Connect(function(player)
local function onClick(click) 
    local RS = game:GetService("ReplicatedStorage")
    local QuirkTrue = Instance.new("BoolValue", player.Backpack)
    if QuirkTrue.Value == true then return end
    if not true then
    local quirk = RS.Quirks.Common
    local quirktable = {}
    quirktable[1] = quirk.Explosion
    quirktable[2] = quirk.Electrification
    quirktable[3] = quirk.Engine
    quirktable[4] = quirk.Quirkless
    quirktable[5] = quirk.Regeneration
    for i = 1, 1 do 
      local localquirk = quirktable[math.random(1,5)]
      local localquirk2 = localquirk:Clone()
      localquirk2.Parent = player.Backpack 
      QuirkTrue.Value = true
    end
end
local function test()
    print("oh uh")
end
test()
script.Parent.ClickDetector.MouseClick:connect(onClick)
end
end)

1 answer

Log in to vote
0
Answered by 5 years ago

Now before I go ahead and answer this: 1. Where is the "script.Parent.ClickDetector" located? (this should be somewhere under Workspace) 2. The main concern I'm seeing just by looking at the code is that the ClickDetector Function is inside the PlayerAdded function, making me think that clicking the Detector won't set off the second function. 3. You've already called the player inside the PlayerAdded so you don't need the "player =" phrase. 4. I tried this out, and found that you can't add a Bool Value to the Backpack unless it was pre-placed inside the "Starter Pack" (the reason for this is the Backpack is meant to carry Tools) so you'll need to add the Bool Value to the Player instead.

With all this in mind, I've adjusted the code like this:

game.Players.PlayerAdded:Connect(function(player)
    local QuirkTrue = Instance.new("BoolValue", player)
    QuirkTrue.Name = "QuirkTrue"
end)

function onClick(click)
    local RS = game:GetService("ReplicatedStorage")
    local QuirkTrue = click.QuirkTrue
    if QuirkTrue.Value == true then return end
    if QuirkTrue.Value ~= true then
        local quirk = RS.Quirks.Common
        local quirktable = {}
        quirktable[1] = quirk.Explosion
        quirktable[2] = quirk.Electrification
        quirktable[3] = quirk.Engine
        quirktable[4] = quirk.Quirkless
        quirktable[5] = quirk.Regeneration
        for i = 1, 1 do
            local localquirk = quirktable[math.random(1,5)]
            local localquirk2 = localquirk:Clone()
            localquirk2.Parent = click
            QuirkTrue.Value = true
        end
    end
end

script.Parent.ClickDetector.MouseClick:connect(onClick)

The first function is the PlayerAdded Function you used, however, it is shortened to only give the Bool Value. The second function is the ClickDetector. Assuming you are using Particles, you would want to replace the following line of code

        localquirk2.Parent = click

with this:

        local char = game.Workspace:FindFirstChild(click.Name)
        local root = char.HumanoidRootPart
        localquirk2.Parent = root

this way, the Particle Emitter would be in the Character, rather than the Player (which is called by "click")

Ad

Answer this question