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.
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)
-- 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)
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
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)