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

What is wrong with this ?

Asked by 9 years ago
brick = script.Parent 
sword = game.Lighting:FindFirstChild("Sword") 

brick.Touched:connect(function (player)  
if player.Character.Humanoid then 
local give = sword:clone()
give.Parent = player.Backpack 
end
end) 

This is a script that give you sword when you touch brick but when i step on the brick nothing happen can someone help ?

2 answers

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

You have a space between the parenthasi and the player parameter in your Anonymous function.

Also the backpack isn't stored in the character, it's in the player. You have to use the GetPlayerFromCharacter method to get the player. Or put this in a localscript and use game.Players.LocalPlayer.Backpack

-Goulstem

0
parenthasi in what line ? decade111 5 — 9y
0
Line 4; brick.Touched:connect(function (player) --Space needs to be removed. Goulstem 8144 — 9y
Ad
Log in to vote
1
Answered by 9 years ago

You have to actually get the player from it's character by using the GetPlayerFromCharacter method in Players, so you can get to the player's backpack.

brick = script.Parent 
sword = game.Lighting:FindFirstChild("Sword") 

brick.Touched:connect(function(hit) --Removed the space, just to be safe.  
    local player = game.Players:GetPlayerFromCharacter(hit.Parent) --Tries to get the player with their character, if the player exists.
    if player then --If the player exists then clone the sword to their backpack.
        local give = sword:clone()
        give.Parent = player.Backpack 
    end
end) 

Answer this question