How to clone an item from ReplicatedStorage to the player's backpack, and how to take a certain amount of money from the player as well

  1. What do you want to achieve? I want that when you touch the part this clone a item from the replicated storage into your backpack, and take the “Required Money” from the player.

  2. What is the issue? It’s somehow not working, evertime I click the part this happends

  3. What solutions have you tried so far? I’ve tried to add the parameter “Player” and call it when the player join, but that didn’t worked, so far.

If is possible, I’d like you guys to tell me what’s wrong, or give me a idea of what can i do to fix this by myself. Thanks and have a good day!

This is the Module

local PlayerReward = {}
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Toolkit = ReplicatedStorage.MainGame.Resources:FindFirstChild("Flash Light")

local RequiredMoney = 5000
local RequiredLevel = 2

---

task.wait(3)
local PlayerStatsFolder = Players:FindFirstChild("leaderstats",true)




function PlayerReward:CheckUser()
	local PlayerMoney = PlayerStatsFolder:FindFirstChild("Money")
	local PlayerLevel = PlayerStatsFolder:FindFirstChild("Level")

	if PlayerMoney.Value >= RequiredMoney and PlayerLevel.Value >= RequiredLevel then
		local GiveItem = Toolkit:Clone()
		GiveItem.Parent = Players[Players.Name].Backpack
		print("Thanks for playing our game! ")
		PlayerMoney.Value = PlayerMoney - RequiredMoney
	else
		return
	end
end



return PlayerReward

This is the client framework:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local MainGame = ReplicatedStorage:WaitForChild("MainGame")
local Modules = MainGame.Modules
local StarterPack = workspace.StarterPack
local Detector = StarterPack.StarterPackDetector

---

local PlayerReward = require(Modules.PlayerReward)

Detector.Touched:Connect(function(FindHumanoid)
	local Humanoid = FindHumanoid.Parent:FindFirstChild("Humanoid")
	
	if Humanoid then
		PlayerReward:CheckUser()
		Detector:Destroy()
	else
		return "I'd like to know, what the is touching me." 
	end
end)

your CheckUser function has no parameter for which player to check

Players.Name is just 'Players', the name of the player service. this is where the ‘which player is being checked’ parameter would come in to specify who you want the function to act on

Again, leaderstats is a child of the individual player, not the Players service.

Try something like this:

function PlayerReward:CheckUser(player)
    local PlayerStatsFolder = player:FindFirstChild('leaderstats')
	local PlayerMoney = PlayerStatsFolder:FindFirstChild("Money")
	local PlayerLevel = PlayerStatsFolder:FindFirstChild("Level")

	if PlayerMoney.Value >= RequiredMoney and PlayerLevel.Value >= RequiredLevel then
		local GiveItem = Toolkit:Clone()
		GiveItem.Parent = player.Backpack
		print("Thanks for playing our game! ")
		PlayerMoney.Value -= RequiredMoney
	else
		return
	end
end

And then in your client framework script you would just call the function according to the player that touched the detector

local Players = game:GetService('Players')

Detector.Touched:Connect(function(FindHumanoid)
	local Humanoid = FindHumanoid.Parent:FindFirstChild("Humanoid")
	
	if Humanoid then
		PlayerReward:CheckUser(Players:GetPlayerFromCharacter(Humanoid.Parent))
		Detector:Destroy()
	else
		return "I'd like to know, what the is touching me." 
	end
end)
2 Likes

Man! Thanks a lot!. It’s now working! Thanks for the help, and have a good day!

1 Like

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