Code issues - trying to give tools to local player

Hello! I’m trying to give the keytool to my player and it doesn’t work, it’s currently a server script, how can I make this work?

my tools are in replicatedstorage and I want to add them to the player’s backpack.

image

script:

-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

-- References
local keyTool = ReplicatedStorage:WaitForChild("Tools").Key_Room1
local remoteEvent = ReplicatedStorage:WaitForChild("GiveKeyToolEvent")

local frameModel = script.Parent.Parent.Frame
local keyModel = script.Parent.Parent.Key
local keyProximityPrompt = keyModel:WaitForChild("keyModelBase"):WaitForChild("keyProximityPrompt")
local frameTriggered = frameModel:WaitForChild("basePart"):WaitForChild("frameProximityPrompt")

-- Function to give KeyTool to a player
local function giveKeyToolToPlayer(player)
	local backpack = player.Backpack
	if backpack then
		local keyToolClone = keyTool:Clone()
		keyToolClone.Parent = backpack
	end
end

-- Connect the function to the frame ProximityPrompt activation
frameTriggered.Triggered:Connect(function()
	print("Frame ProximityPrompt Triggered")
	wait(0.2)
	frameModel:Destroy()
	wait(0.5)
	keyProximityPrompt.Enabled = true
end)

-- Connect the function to the key ProximityPrompt activation
keyProximityPrompt.Triggered:Connect(function(player)
	print("Key ProximityPrompt Activated")
	wait(0.5)
	keyModel:Destroy()

	-- Wait for a short time before giving the KeyTool to the player
	task.wait(1)

	-- Ensure that the local player is available before giving the KeyTool
	local localPlayer = Players.LocalPlayer
	if localPlayer then
		giveKeyToolToPlayer(localPlayer)
	end
end)

Since it is a Server Script, you can’t use LocalPlayer directly.

Luckily you already have the parameter for it to get the specific “Local” player which is in the function(player).

Therefore you can just:

keyProximityPrompt.Triggered:Connect(function(player)
    local localPlayer = player
    print("This player triggered the proximity: " + localPlayer.Name)
end)

i tried this and got an error :frowning:

-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

-- References
local keyTool = ReplicatedStorage:WaitForChild("Tools").Key_Room1
local remoteEvent = ReplicatedStorage:WaitForChild("GiveKeyToolEvent")

local frameModel = script.Parent.Parent.Frame
local keyModel = script.Parent.Parent.Key
local keyProximityPrompt = keyModel:WaitForChild("keyModelBase"):WaitForChild("keyProximityPrompt")
local frameTriggered = frameModel:WaitForChild("basePart"):WaitForChild("frameProximityPrompt")

-- Function to give KeyTool to a player
local function giveKeyToolToPlayer(player)
	local backpack = player.Backpack
	if backpack then
		local keyToolClone = keyTool:Clone()
		keyToolClone.Parent = backpack
	end
end

-- Connect the function to the frame ProximityPrompt activation
frameTriggered.Triggered:Connect(function()
	print("Frame ProximityPrompt Triggered")
	wait(0.2)
	frameModel:Destroy()
	wait(0.5)
	keyProximityPrompt.Enabled = true
end)

-- Connect the function to the key ProximityPrompt activation
keyProximityPrompt.Triggered:Connect(function(player)
	print("Key ProximityPrompt Activated")
	wait(0.5)
	keyModel:Destroy()

	-- Wait for a short time before giving the KeyTool to the player
	task.wait(1)

	-- Ensure that the local player is available before giving the KeyTool
	local localPlayer = player
	print("This player triggered the proximity: " + localPlayer.Name)
	if localPlayer then
		giveKeyToolToPlayer(localPlayer)
	end
end)


keyProximityPrompt.Triggered:Connect(function(player)
print(“Key ProximityPrompt Activated”)
wait(0.5)
keyModel:Destroy()

task.wait(1)

giveKeyToolToPlayer(player)
end)

same error :frowning:

15:09:41.267 Workspace.Escene.PropsGame.Key.Script:43: attempt to perform arithmetic (add) on string - Studio

basically Players.LocalPlayer doesn’t exist on the server, at all
so you’re gonna have to

keyProximityPrompt.Triggered:Connect(function(player)
	print("Key ProximityPrompt Activated")
	wait(0.5)
	keyModel:Destroy()

	-- Wait for a short time before giving the KeyTool to the player
	task.wait(1)

	-- Ensure that the local player is available before giving the KeyTool
	print("This player triggered the proximity: " .. player.Name)
	giveKeyToolToPlayer(player)
end)

basically you gotta use .. instead of + when combining strings together
this also kinda solves the issue of it just not working in general

2 Likes

Now it prints it but I can’t give the tool to the player, what can I do? :frowning:

image

maybe instead of having a completely new function for giving the key to the player, you could possibly just like

print("This player triggered the proximity: " .. player.Name)
keyTool:Clone().Parent = 
    player:FindFirstChildOfClass("Backpack") or player:WaitForChild("Backpack")

might be a stupid thing to do but eh it could probably work

It worked! I was able to solve it, thank you very much!

-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

-- References
local keyTool = ReplicatedStorage:WaitForChild("Tools").Key_Room1
local remoteEvent = ReplicatedStorage:WaitForChild("GiveKeyToolEvent")

local frameModel = script.Parent.Parent.Frame
local keyModel = script.Parent.Parent.Key
local keyProximityPrompt = keyModel:WaitForChild("keyModelBase"):WaitForChild("keyProximityPrompt")
local frameTriggered = frameModel:WaitForChild("basePart"):WaitForChild("frameProximityPrompt")

-- Function to give KeyTool to a player
local function giveKeyToolToPlayer(player)
	local backpack = player.Backpack
	if backpack then
		local keyToolClone = keyTool:Clone()
		keyToolClone.Parent = backpack
	end
end

-- Connect the function to the frame ProximityPrompt activation
frameTriggered.Triggered:Connect(function()
	print("Frame ProximityPrompt Triggered")
	wait(0.2)
	frameModel:Destroy()
	wait(0.5)
	keyProximityPrompt.Enabled = true
end)

-- Connect the function to the key ProximityPrompt activation
keyProximityPrompt.Triggered:Connect(function(player)
	print("Key ProximityPrompt Activated")
	wait(0.5)
	keyModel:Destroy()

	-- Ensure that the local player is available before giving the KeyTool
	print("This player triggered the proximity: " .. player.Name)
	giveKeyToolToPlayer(player)
end)
1 Like

Replace the reference with this

local keyTool = ReplicatedStorage:WaitForChild(“Tools”)[“Key_Room1”]

that just does the same thing as before, just with a different way of indexing the tool

1 Like

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