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

My knife give script only works sometimes. It's confusing. Help?

Asked by 4 years ago

So I'm making a game and you're supposed to get a knife to beat a boss. You get the knife by touching an invisible part. The thing is, it only works half the time. Sometimes it works, other times it doesn't. My knife is located in ServerStorage. Here's the code:

local db = true

script.Parent.Touched:Connect(function(hit)
    db = false
    if db == false then
        wait(0.1)
        local knife = game.ServerStorage:WaitForChild("Knife")
        local knifer = knife:Clone()
        knifer.Parent = hit.Parent
        script.Parent:Destroy()
        wait(1)
        db = true
    end
end)

Is there anything I'm doing wrong? It's making my mind explode right now, so please help.

0
Why did you add a debounce? http_shawn 1 — 4y
0
Because it would give me multiple knives at once. KyofuOmo 25 — 4y
0
You just can't hit properly I am guessing hAhaHaHaHhhAahhaHAHhAhaHaHhahAhhahhAhaHaHahhAhaHaHaHhaH greatneil80 2647 — 4y
0
never use wait() in a debounce. greatneil80 2647 — 4y
0
unless you are toggling it only as the inverse. greatneil80 2647 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

I dont know what's wrong with the script (like http_shawn) but I have experience with this.

1) Try putting the knife into the ReplicatedStorage, sometimes the ServerStorage doesn't work with duplication ;-;

2) add the knife duplication to the player's Backpack. It can be done like so:

local db = true

script.Parent.Touched:Connect(function(hit)
    if db == true and hit.Parent:FindFirstChild("Humanoid") then
    db = false
            wait(0.1)
            local knife = game.ReplicatedStorage:WaitForChild("Knife")
            local knifer = knife:Clone()
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
            knifer.Parent = player.Backpack
            script.Parent:Destroy()
            wait(1)
            db = true
    end
end)

I also made a few modifications listed below:

1) added "and hit.Parent:FindFirstChild("Humanoid") " to ensure that whatever touched the invisible block is a player

2) changed how you ordered the debounce (if that makes sense)

3) changed knife parent to Replicated Storage

4) changed knife duplication to player's Backpack

Hope this helps!

Ad
Log in to vote
0
Answered by 4 years ago

I don't really know what is wrong with the script, though I wouldn't do a debounce instead I would use an in pairs thingy

script.Parent.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    local finder = player.Backpack:FindFirstChild("Knife")
if not finder then
        local knife = game.ServerStorage:WaitForChild("Knife")
        local knifer = knife:Clone()
        knifer.Parent = hit.Parent
        script.Parent:Destroy()
end
end)

Hope this works!

Answer this question