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

My touched script won't work?

Asked by
Relatch 550 Moderation Voter
9 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)

2 answers

Log in to vote
1
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
9 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 — 9y
0
Thanks! Relatch 550 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

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 4330 — 9y
0
You can actually get a player by using "LocalPlayer". It just need's to be in a local script. EzraNehemiah_TF2 3552 — 9y
0
You can, but not directly from a Touched event. (Btw, I didn't down you) Shawnyg 4330 — 9y
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 — 9y

Answer this question