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

How to make a chatted event give a player a tool?

Hello, I am trying to make it so that when a player says a specific phrase they are given a tool which comes from ServerStorage.

So far this is my script.

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg, recipient, speaker)
		
		if string.lower(msg) == "test" then -- checks if the message was test being not capital specific
			local toolClone = game.ServerStorage["Gold Key"]:Clone() -- Directory to tool and then clones it
			toolClone.Parent = speaker.Backpack -- Puts clone into backpack (this is where the error is coming from)
		end
		
	end)
end)

In it's current state when a player says "test" an error appears saying attempt to index nil with 'Backpack.

How would I go about fixing this issue?

1 answer

✓ Accepted answer

Why would you need to get speaker once you've already defined the player through PlayerAdded?

local Players = game:GetService('Players')
local serverStorage = game:GetService('ServerStorage')

Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message)
		if string.lower(Message) == 'test' then
			local toolClone = serverStorage['Gold Key']:Clone()
			toolClone.Parent = Player.Backpack
		end	-- message check
	end) -- chatted event
end) -- playerAdded, defined player

1 comment

0

I didn't even think of that, thank you so much!

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