Cant figure this out

Hello! I’ve been trying to make a system where players can collect gifts and when they trigger a proximityPrompt, it converts the gifts in coins. It shouldnt be this hard but every time it returns 0 coins. This is my server script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local HousesFolder = workspace:WaitForChild("Game"):WaitForChild("Systems"):WaitForChild("Houses")

-- Remotes \\ 
local MessagesEvent = ReplicatedStorage.Remotes:WaitForChild("MessagesEvent")
local PlaceGiftEvent = ReplicatedStorage.Remotes:WaitForChild("PlaceGiftEvent")
local TriggerAnimEvent = ReplicatedStorage.Remotes:WaitForChild("TriggerAnimEvent")
local CameraShakeEvent = ReplicatedStorage.Remotes:WaitForChild("CameraShakeEvent")
-- // 

local parentModel = script.Parent
local ChristmasTree = parentModel.Interior.Systems.ChristmasTree
local GiftsFolder = ChristmasTree:FindFirstChild("Gifts")

local function teleportPlayer(player, targetPart)
	local character = player.Character
	if character and character:FindFirstChild("HumanoidRootPart") then
		character:SetPrimaryPartCFrame(targetPart.CFrame)
		print("Teleported " .. player.Name)
	end
end

local function createLivesValue(player)
	local lives = Instance.new("IntValue")
	lives.Name = "Lives"
	lives.Value = 3
	lives.Parent = player
end

local function createPlacedGiftsValue(player)
	local placedGifts = Instance.new("NumberValue")
	placedGifts.Name = "PlacedGifts"
	placedGifts.Value = 0
	placedGifts.Parent = player
end

local function destroyLivesValue(player)
	local lives = player:FindFirstChild("Lives")
	if lives then
		lives:Destroy()
	end
end

local function shakeCamera(player)
	CameraShakeEvent:FireClient(player)
end

local playerCooldowns = {}

local function onPlayerTouchDamagePart(player)
	if playerCooldowns[player.UserId] then
		return
	end

	local lives = player:FindFirstChild("Lives")
	if lives and lives.Value > 0 then
		local isLastLife = (lives.Value == 1)

		if not isLastLife then
			playerCooldowns[player.UserId] = true
		end

		lives.Value = lives.Value - 1
		print(player.Name .. " lost a life. Lives left: " .. lives.Value)

		shakeCamera(player)

		if lives.Value <= 0 then
			destroyLivesValue(player)
			
			print(player.Name .. " has been teleported out due to 0 lives.")

			TriggerAnimEvent:FireClient(player)
			wait(1)
			local teleporterPart = script.Parent.Teleporter
			teleportPlayer(player, teleporterPart)
		else
			wait(2)
			playerCooldowns[player.UserId] = nil
		end
	end
end

script.Parent:WaitForChild("Teleporter").ProximityPrompt.Triggered:Connect(function(player)
	local placedGifts = player:FindFirstChild("PlacedGifts")
	local playerBackpack = player:FindFirstChild("PlayerBackpack")
	local giftCount = playerBackpack:FindFirstChild("GiftCount")


	if not placedGifts then
		createPlacedGiftsValue(player)
		placedGifts = player:WaitForChild("PlacedGifts")
	end

	if giftCount and giftCount.Value > 0 then
		local houseNumber = script.Parent.Name
		local interiorModel = HousesFolder:FindFirstChild(houseNumber):FindFirstChild("Interior")
		local spawnPart = interiorModel:FindFirstChild("SpawnPart")
		local exitPart = interiorModel:FindFirstChild("ExitPart")
		local teleportPart = script.Parent.Teleporter

		if spawnPart then
			TriggerAnimEvent:FireClient(player)
			wait(1)
			teleportPlayer(player, spawnPart)

			if not player:FindFirstChild("Lives") then
				createLivesValue(player)
			end

			local damagePartsFolder = interiorModel.Systems:FindFirstChild("DamageParts")
			if damagePartsFolder then
				for _, damagePart in ipairs(damagePartsFolder:GetDescendants()) do
					if damagePart:IsA("BasePart") then
						damagePart.Touched:Connect(function(hit)
							local touchedPlayer = game.Players:GetPlayerFromCharacter(hit.Parent)
							if touchedPlayer and touchedPlayer == player then
								onPlayerTouchDamagePart(player)
							end
						end)
					end
				end
			end
		end

		exitPart.ProximityPrompt.Triggered:Connect(function()
			local coins = player.leaderstats:FindFirstChild("Coins")
			if coins then
				coins.Value = coins.Value + placedGifts.Value
				print("Player will be rewarded with " .. placedGifts.Value .. " coins.")
			end
			
			TriggerAnimEvent:FireClient(player)
			wait(1)
			destroyLivesValue(player)
			placedGifts:Destroy()
			teleportPlayer(player, teleportPart)
		end)
	else
		MessagesEvent:FireClient(player, "0GiftsMessage")
		print(player.Name .. " has no gifts and cannot teleport.")
	end
end)


ChristmasTree.PlacePart.ProximityPrompt.Triggered:Connect(function(player)
	local houseNumber = script.Parent.Name
	PlaceGiftEvent:FireClient(player, houseNumber)
end)

image

In my player, the placedGifts value is fine. Ive been stuck on this for like 1 1/2 hours, and i cant take it anymore, this was why i came here :pray:

1 Like

are you confusing “giftCount” value with “placedGifts”? it seems like a possibility.
in this line:

coins.Value = coins.Value + placedGifts.Value

try substituting it with the “giftCount” value.

it could also be because you are refreshing the value to 0 at this point in the script where you double check to make sure the value exists:

if not placedGifts then
		createPlacedGiftsValue(player)
		placedGifts = player:WaitForChild("PlacedGifts")
	end
2 Likes

this is refreshing the value?? i mean it would make sense, since when i change the 0 to a 3 for example, i get rewarded with 3 coins:

local function createPlacedGiftsValue(player)
	local placedGifts = Instance.new("NumberValue")
	placedGifts.Name = "PlacedGifts"
	placedGifts.Value = 0
	placedGifts.Parent = player
end

How else should i change it to make it not refresh?

2 Likes

well you see what you’re doing is overwriting whatever amount you have before hand by calling this function before you add the coins. does that make sense?

you’re saying

if not placedGifts then
		createPlacedGiftsValue(player)
		placedGifts = player:WaitForChild("PlacedGifts")
	end

right before you add the values. i think what this is doing is resetting the value, so maybe try removing it.

This is not “refreshing” your value because you check if the player has the numberValue or not before creating a new one. if it was actually “refreshing” your values then you would need to set its value to 0, which you don’t do

2 Likes

i’m using “refreshing” when i guess i should be saying “resetting”. i think the point he’s at in the script right before he adds the value to the coins is setting whatever he has back to 0.

the function “createPlacedGiftsValue(player)” is changing the value to 0. if he already collected some gifts, say he collected 1, that 1 would turn into a 0 and he wouldn’t get any coins.

No. The function just creates a new numberValue it doesn’t modify anything. The part of the code you highlighted just checks if the player has the numberValue and if not it creates a new one because it doesn’t exist

2 Likes

alright. my assumption was that there’s already a value and by using that function it’s overwriting the current one.

nevermind, i see it now. i was skimming through his script mostly, my mistake :sweat_smile:

sorry to interrupt, but im honestly lost to what i should do, just remove it? :sweat_smile:

You never set the placedGifts value anywhere in the code. All I see is you creating and getting the value but never setting it.

No don’t remove that line.

FillerFillerFiller

wdym exactly by setting the placedGifts value?

Like modifying its value to match what you want. Something like placedGifts.Value += 1

i mean technically the match i wanted really was just to add the placedGifts count to the coins.

You can visualise this better in game:

anyone down to help? still stuck on this :sweat_smile:

You aren’t increasing the PlacedGifts value anywhere…?

Are you changing the value of the client? I wasn’t able to help earlier because I was doing something mb for leaving you hanging

Edit: You need to change the numberValue’s value on the server because on the server the value is always 0. Client changes don’t replicate to the server remember that.