No energy drink(((

Hello. According to the plan, when I trigger the proximity prompt, I should receive an energy drink, but nothing happens. Am I doing something wrong or am I stupid. There are no errors or prints in the output. This is a local script and the energy drink must be received by the player who triggered the proximity prompt. Some screenshots

image

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local EnergyDrink = ServerStorage:WaitForChild("Energy Drink")
local player = Players.LocalPlayer

local CanGiver = script.Parent
local proximityPrompt = CanGiver

local function CanGive(player)
	local inventory = player:FindFirstChild("Backpack") or player:WaitForChild("Backpack")

	if inventory and EnergyDrink then
		local energyDrinkClone = EnergyDrink:Clone()
		energyDrinkClone.Parent = inventory
		print("Energy drink given to player:", player.Name)
	else
		print("Couldn't find backpack or Energy Drink.")
	end
end

if proximityPrompt then
	proximityPrompt.Triggered:Connect(function()
		CanGive(player)
		print("Proximity prompt triggered.")
	end)
else
	warn("ProximityPrompt not found in door.")
end
2 Likes

If it’s a local script I’d suggest moving it to StarterPlayerScripts.

it would probably be a better idea to just use the one server script to avoid several scripts and problems with client replication, plus i dont think you can access ServerStorage from the client (which if you really wanted to you could just use ReplicatedStorage)

if you are using a server script, the ProximityPrompt instance returns “playerWhoTriggered”, which you can use to give the player their energy drink like this:

if proximityPrompt then
	proximityPrompt.Triggered:Connect(function(player) -- Player: Player who activated prompt
		CanGive(player)
		print("Proximity prompt triggered.")
	end)
else
	warn("ProximityPrompt not found in door.")
end

You shouldn’t be using a LocalScript for that.

I also recommend checking whether the player has an energy drink already, or just adding a debounce.

Put a normal script where the local script is and try this:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local EnergyDrink = ServerStorage:WaitForChild("Energy Drink")

local proximityPrompt = script.Parent

local function giveCan(player)
	local inventory = player:FindFirstChild("Backpack")
	
	if inventory and EnergyDrink then
		local energyDrinkClone = EnergyDrink:Clone()
		energyDrinkClone.Parent = inventory
		print("Energy drink given to player:", player.Name)
	else
		print("Couldn't find backpack or Energy Drink.")
	end
end

if proximityPrompt then
	proximityPrompt.Triggered:Connect(function(player)
		giveCan(player)
	end)
else
	warn("ProximityPrompt not found in door.")
end

It works correctly. Thank you so much!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.