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

How do i give the player an item when they say something?

Asked by 1 year ago

I dont understand, i tried to give the player something when they say "sword" but it did not work

Code:

game.Players.PlayerAdded:Connect(function(player) 
    player.Chatted:Connect(function(msg) 
        if msg == "sword" then 

            local original = game.ServerStorage.R6Sword
            local copy = original:Clone()
            copy.Parent = game.Players.LocalPlayer.Backpack

        end
    end)
end)

1 answer

Log in to vote
1
Answered by 1 year ago

Well first thing, I see you're using LocalPlayer, there is 2 problems with this:

  1. If you give the tool using a LocalScript, it will not be replicated in the server
  2. You're using ServerStorage, LocalScripts cannot access ServerStorage

Plus, it isn't necessary, as we already have the player variable

So instead, put your code in a normal script, here's a version that should work:

local CallMsg = "sword"

local item = game.ServerStorage.R6Sword

game.Players.PlayerAdded:Connect(function(player) 
    player.Chatted:Connect(function(msg) 
        if msg == CallMsg then 

            local original = item
            local copy = original:Clone()
            copy.Parent = player.Backpack

        end
    end)
end)

You can place this script in ServerScriptService if you'd like

Oh and btw, I recommend using ReplicatedStorage to store tools.

1
Thanks a lot! PeterParker17386 16 — 1y
Ad

Answer this question