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

This script is about touching a part and then getting a sword but it doesnt work,why?

script.Parent.Touched:Connect(function(hit)
	if game.Players:GetPlayerFromCharacter(hit.Parent)
		then hit.Parent.Part.Sword.Equipped=true
		
	end	
	end)

2 comments

0

please fix the lua box.

0

what is a lua box?

1 answer

✓ Accepted answer

Sometimes the hit part's Parent will not be your Character ( Handles of accessories for example, have their Parent as said accessory, and not your Character ) for this reason it's safer to first check if it has a Humanoid as a Parent, and then, use GetPlayerFromCharacter , like this:

Players = game:GetService("Players")  --Gets Player service
Rep = game.ReplicatedStorage
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then --Checks if Toucher has a Humanoid
local Player = Players:GetPlayerFromCharacter(hit.Parent) -- Gets Player 
if Rep:FindFirstChild("Sword") and not Player.Character:FindFirstChild("Sword") then
local Sword = Rep.Sword:Clone()
Sword.Parent = Player.Character
end
end
end)

But this might not be the only problem, you should have to check the Tool's name, if it exists, and more to make sure this works properly.

Also, make sure you put all of your code inside a lua Box, like this:

"~~~~~~~~~~~~~~~~~"

--Code

"~~~~~~~~~~~~~~~~~"

( Without the quotations, of course, it would look this: )

--Code

Hope this helps!

5 comments

0

this doesnt work

0

What error is it giving? i might be able to fix it

0

The error is: Part is not a valid member of Model

0

Ok, i edited my script. Now try putting your tool in the ReplicatedStorage and making sure it has Sword as its name, and it should equip it just fine

0

Thanks Dude

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