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

[Solved]Why won't it work?

Asked by
22To 70
9 years ago

Ok, I made the weapon spawner script over and i added the cooldown with the debounce but the script wont work whats the problem?

ToolName = "TMP" -- Exact name of the weapon/tool
RespawnTime = 6
Debounce = false
script.Parent.Touched:connect(function(hit)
if Debounce == false then
Debounce = true
p = game.Players:playerFromCharacter(hit.Parent)
        if p == nil then return end
else
Wep = game.Lighting.TMP:Clone() --Change TMP to the name of your weapons
        if p.Backpack:findFirstChild(Wep.Name) ~= nil then return end
else
        Wep.Parent = p.Backpack
end
end
wait(RespawnTime)
Debounce = false
end
end)

0
Can you add spacing please! dragonkeeper467 453 — 9y
0
spacing where? 22To 70 — 9y

2 answers

Log in to vote
0
Answered by
Redbullusa 1580 Moderation Voter
9 years ago

Indent in a more organized fashion! You'll be denoted as a more organized and professional scripter.

ToolName = "TMP" -- Exact name of the weapon/tool
RespawnTime = 6
Debounce = false

script.Parent.Touched:connect(function(hit)
    if Debounce == false then
        Debounce = true
        p = game.Players:playerFromCharacter(hit.Parent)
        if p == nil then
            return
        end
    else
                Wep = game.Lighting.TMP:Clone() --Change TMP to the name of your weapons
                if p.Backpack:findFirstChild(Wep.Name) ~= nil then
                    return
                end
            else
                Wep.Parent = p.Backpack
            end
        end
        wait(RespawnTime)
        Debounce = false
    end
end)
-- I just realized your script contained an error. Nope nope nope nope nope nope...

Let's redo the script. The objective here is to, given the ToolName, clone the desired weapon and place it in the toucher's backpack - assuming that it is a player.

ToolName = "TMP"
RespawnTime = 6
Debounce = false

script.Parent.Touched:connect(function (Hit)
    if not Debounce then
        Debounce = true
        -- Where the cloning takes place
        wait(RespawnTime)
        Debounce = false
    end
end)

Let's wrap the "if" statement (with the Debounce as its condition) into another "if" statement that makes sure that the toucher is a) a valid character, b) is part of a player instance, and c) is (preferably) alive.

ToolName = "TMP"
RespawnTime = 6
Debounce = false

script.Parent.Touched:connect(function (Hit)
    local HumanoidIsFound = Hit.Parent:FindFirstChild("Humanoid")
    local PlayerIsFound = game.Players:GetPlayerFromCharacter(Hit.Parent)
    if Hit.Parent and HumanoidIsFound and HumanoidIsFound.Health > 0 and PlayerIsFound then
        if not Debounce then
            Debounce = true
            local Weapon = game.ServerStorage:FindFirstChild(ToolName)
            if Weapon then
                local newWeapon = Weapon:Clone()
                newWeapon.Parent = PlayerIsFound.Backpack
            else
                print("No such weapon named as " .. ToolName .. " in game.ServerStorage.")
                Debounce = false
                return
                -- In my opinion, lines 15-18 are pointless, but it's good to know what's wrong with your script. This is here just in case if the weapon does not exist.
            end
            wait(RespawnTime)
            Debounce = false
        end
    end
end)

Any questions? Comments? Feel free to ask.

0
Thanks :D and thanks for explaining also now i get the point 22To 70 — 9y
0
You're welcome Redbullusa 1580 — 9y
Ad
Log in to vote
0
Answered by
iNicklas 215 Moderation Voter
9 years ago

Is it a part you need to touch and you'll get a weapon?

0
yes 22To 70 — 9y

Answer this question