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

My touched script won't work?

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

✓ Accepted answer

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)

2 comments

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"...

0

Thanks!

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)

4 comments

0

The script isn't a LocalScript, and that's not how to get the Player.

0

You can actually get a player by using "LocalPlayer". It just need's to be in a local script.

0

You can, but not directly from a Touched event. (Btw, I didn't down you)

0

Oops. Thinking of a gui. I've been making gui's for so you know. I got used to Local Player... Oops. :\

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