0

# My touched script won't work?

Relatch 550
7 years ago

It works up until the "sword.Parent = hit.Parent.Backpack" part. Here's the error: Backpack is not a valid member of Model

part = script.Parent
serverstorage = game:GetService("ServerStorage")

part.Touched:connect(function(hit)
part.BrickColor = BrickColor.new("Really red")
local sword = serverstorage:WaitForChild("Sword"):Clone()
sword.Parent = hit.Parent.Backpack
end)


1
Shawnyg 4300
7 years ago

Backpack is a descendant of a Player.

part = script.Parent
serverstorage = game:GetService("ServerStorage")

part.Touched:connect(function(hit)
if game.Players:GetPlayerFromCharacter(hit.Parent) then
p = game.Players:GetPlayerFromCharacter(hit.Parent)
part.BrickColor = BrickColor.new("Really red")
local sword = serverstorage:WaitForChild("Sword"):Clone()
sword.Parent = p.Backpack
end
end)


0
I think that it's better to put it in a local script and use Players.LocalPlayer because It doesn't always work with "hit.Parent.Name"... EzraNehemiah_TF2 3552 — 7y
0
Thanks! Relatch 550 — 7y
0

You had a little mistake. Backpack isn't in the character. It's in the player.

THIS SCRIPT IS IN A LOCAL SCRIPT

part = script.Parent
serverstorage = game:GetService("ServerStorage")

part.Touched:connect(function(hit)
part.BrickColor = BrickColor.new("Really red")
local sword = serverstorage:WaitForChild("Sword"):Clone()
sword.Parent = game.Players.LocalPlayer.Backpack --Backpack is in players.
end)


0
The script isn't a LocalScript, and that's not how to get the Player. Shawnyg 4300 — 7y
0
You can actually get a player by using "LocalPlayer". It just need's to be in a local script. EzraNehemiah_TF2 3552 — 7y
0
You can, but not directly from a Touched event. (Btw, I didn't down you) Shawnyg 4300 — 7y
0
Oops. Thinking of a gui. I've been making gui's for so you know. I got used to Local Player... Oops. :\ EzraNehemiah_TF2 3552 — 7y