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.
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!
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!