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

Script doesn't work as planned - Won't give tool, help?

Asked by 7 years ago

Hello. I am currently making a game which is taking a LOT of coding, therefore I have a TON of errors. This time, I made a script where when the character touches a part of a model, it gives the player a tool. I do not want anything else but a fix for this script.

Here is the script:

db = false
function OnTouched(hit)
    if hit.Parent:FindFirstChild("Humanoid") and db == false then
        db = true
        local wep = script.Parent.Parent--(change this part if needed, needs to find the name of the model)
        local clone = game.ReplicatedStorage.Weapons:FindFirstChild(wep.Name):Clone()
        clone.Parent = game.Players:FindfirstChild(hit.Parent.Name).Backpack
        wep:remove()
        return
        db == false
    end
end

script.Parent.Touched:connect(OnTouched)

Extra Information:

The tool is inside a folder called "Weapons", which is in the ReplicatedStorage. The tool and model have the same name. The model contains two parts: one for the mesh and one for the script. The script part is larger than the mesh part. Both parts have CanCollide off.

And, Thanks!

0
Is this this a regular script or local script, and also is this FE? Crazycat4360 115 — 7y
0
Remove the return statement inside the function. It is not needed. RayCurse 1518 — 7y
0
Crazycat, it's a regular script. No FE, but planning on changing it later. Zeref_DragneelFT 8 — 7y

1 answer

Log in to vote
0
Answered by
Astralyst 389 Moderation Voter
7 years ago

Try this instead.

db = false
script.Parent.Touched:connect(function(hit)
    player = game.Players:GetPlayerFromCharacter(hit.Parent) -- finds player
    if player and player:IsA("Player") and db == false then -- you don't have to check for Humanoid, just check if it's a Player.
    db = true
        local wep = script.Parent.Parent
        local clone = game.ReplicatedStorage.Weapons:FindFirstChild(wep.Name):Clone()
        clone.Parent = player.Backpack
db = false
    end
end)
0
By the way, remember that ROBLOX is case senstive. Line 7 in your script, "FindfirstChild" should be "FindFirstChild". That might be the reason why your script broke in the first place. Astralyst 389 — 7y
0
Oh. Thanks! Zeref_DragneelFT 8 — 7y
Ad

Answer this question