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

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

Asked by 4 years ago
Edited 4 years ago
script.Parent.Touched:Connect(function(hit)
    if game.Players:GetPlayerFromCharacter(hit.Parent)
        then hit.Parent.Part.Sword.Equipped=true

    end 
    end)
0
please fix the lua box. NarwhalAndMe 141 — 4y
0
what is a lua box? SpectacularPavlos 86 — 4y

1 answer

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

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!

0
this doesnt work SpectacularPavlos 86 — 4y
0
What error is it giving? i might be able to fix it bum5Br 97 — 4y
0
The error is: Part is not a valid member of Model SpectacularPavlos 86 — 4y
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 bum5Br 97 — 4y
0
Thanks Dude SpectacularPavlos 86 — 4y
Ad

Answer this question