Send a event based on item in inventory

I’m trying to make a script where it detects what item you have in your inventory and based on that it will open a door or send a event.

--This is the local script
print("locals found ls")
local ReplicatedService = game:GetService("ReplicatedStorage");
local plyr = game:GetService("Players").LocalPlayer
local event = ReplicatedService:WaitForChild("X");
print("locals Found ls")

script.Parent.ProximityPrompt.Triggered:Connect(function()
	event:FireServer();
end)
--This is the server script
print("Finding locals ss")
local Replicated = game:GetService("ReplicatedStorage");
local event = Instance.new("RemoteEvent", Replicated)
event.Name = "X"
print("Found Locals ss")

event.OnServerEvent:connect(function(player)
	local allowed = player.Backpack.FindFirstChild("X")
	if allowed then
		print("Allowed")
	else
		print("Not allowed")
	end
end)

This is also how I have the local script ordered.
RobloxStudioBeta_f3VoJpPNn3

1 Like

In this part of your script, a colon must be used before FindFirstChild, not a period.

local allowed = player.Backpack:FindFirstChild("X")

Hope this helps!

Also, if you will be trying to use a proximity prompt for your game, please do note that it’s in beta phase and will only be available in Studio, unless Roblox makes it live.

1 Like

ProximityPrompt CANNOT be in a local script. It MUST be in a server script parented to a part.

1 Like

You can listen a proximity prompt’s triggered event through local scripts as stated in Proximity Prompt Studio Beta:

1 Like

I tried replicating your problem and it seems that Local scripts will not run when they are parented to the workspace.

I recommend placing the local script under StarterPlayerScripts which is parented to the StarterPlayer folder. Then, change the reference for the Proximity Prompt (workspace.Part.ProximityPrompt). Don’t forget to place a colon, instead of a period, before FindFirstChild.

1 Like