Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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!

3 comments

0

Is this this a regular script or local script, and also is this FE?

0

Remove the return statement inside the function. It is not needed.

0

Crazycat, it's a regular script. No FE, but planning on changing it later.

1 answer

✓ Accepted answer

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)

2 comments

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.

0

Oh. Thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.