Is there a way to make a part that gives currency on another game? (or merge 2 scripts)

Hi! I just wanted to know if theres a way to a part give certain currency on different games or a way to merge my 2 scripts. Im new at scripting, so sorry if that issue sounds dumb.

image

My idea its if you win the obby, you get Coins in the Main Game, but i cant do that. I tried to put the same leaderstats for each game, but i have a script that creates another leaderstats for the checkpoints, and they dont work together.
The main issue its that if i put the 2 scripts together, the leaderstats dont show.

Thanks!

2 Likes

The best way to do this would be to use MessagingService.

You should be able to use both scripts in the separate game and main game. When you do that the datastore is in the same universe and should work.

Then as Kaiden said use the MessagingService to send the amount of Coins you got from the obby, back with you to the main game.

i did that, i used my leaderstats script in both games, and as i said the leaderstats didn’t show up in the game.

ill try to find videos about that, thanks!

Np! An example of using the TeleportService (which in my opinion I would use that) is this.

local Part = script.Parent
local TeleportService = game:GetService("TeleportService")
local placeId = 0 -- change this to place id

local function changePlace(otherPart)
	local humanoid = otherPart.Parent:FindFirstChild("Humanoid")
	if humanoid then
		local player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
		if player then
			local data = {}
			data["coins"] = "AmountOfCoins"
			TeleportService:Teleport(placeId, player, data)
		end
	end
end
Part.Touched:Connect(changePlace)

This is a script in a part, when the player touches it, it sends them to the place id with the data. This youtube video could help you! video

if i use this script i will need to add a leaderstats at the other game script like that?

local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local Stage = Instance.new("IntValue", leaderstats)
	Stage.Name = "Stage"
	Stage.Value = stage

    local Coins = Instance.new("IntValue", leaderstats)
   	Coins.Name = "Coins"
	Coins.Value = 0

Yes that is correct. You can just make a leaderstats script and then just copy it over.

it didnt worked, what exactly i need to do?

Ok so here is what you need to do. In Main Game AND Obby I want you to make a script and put it in SSS. It will be your player stats (Make sure to have API Access enabled in game settings). Below is the script.

local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("RagdollCenter")



local function onPlayerJoin(player)
	
	local PlayerStats = Instance.new("Folder")
	PlayerStats.Name = "PlayerStats"
	PlayerStats.Parent = player
	
	--//Stats\\--

	local Coins = Instance.new("NumberValue")
	Coins.Name = "Coins"
	Coins.Parent = PlayerStats
	
	local Stage = Instance.new("NumberValue")
	Stage.Name = "Stage"
	Stage.Parent = PlayerStats


	local playerUserId = "Player_" .. player.UserId
	local data = playerData:GetAsync(playerUserId)

	if data then

		Coins.Value = data['Coins']
		Stage.Value = data['Stage']
	else

		Coins.Value = 0
		Stage.Value = 0
	end
end


local function create_table(player)

	local player_stats = {}

	for _, stat in pairs(player.PlayerStats:GetChildren()) do

		player_stats[stat.Name] = stat.Value
	end

	return player_stats
end


local function onPlayerExit(player)


	local player_stats = create_table(player)
	local success, err = pcall(function()

		local playerUserId = "Player_" .. player.UserId

		playerData:SetAsync(playerUserId, player_stats) 
	end)


	if not success then

		warn('Could not save data!')
	end
end


game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)

Then make a part, add a script, and put this. To get the subgames id, go to asset manager, right click on the place, and copy id to clipboard.

local Part = script.Parent
local TeleportService = game:GetService("TeleportService")
local placeId = 8571558596 -- change this to place id

local function changePlace(otherPart)		
	local humanoid = otherPart.Parent:FindFirstChild("Humanoid")
	
	if humanoid then			
		local player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
		local playerStats = player:WaitForChild('PlayerStats')
		local Coins = playerStats:WaitForChild('Coins')
		local AmountOfCoins = Coins.Value	
		
		if player then			
			local data = {}
			data["coins"] = AmountOfCoins
			TeleportService:Teleport(placeId, player, data)
		end
	end
end
Part.Touched:Connect(changePlace)

Then make a ScreenGui, TextLabel, and put a local script in the TextLabel. This goes in your subgame Obby.

local TeleportService = game:GetService("TeleportService")
local data = TeleportService:GetLocalPlayerTeleportData()

local Label = script.Parent

if data then
	
	local coins = data["coins"]
	Label.Text = coins
else
	
	Label.Text = "No Data"
end

If you want to change the players coins then just put a part into studio, then put a ClickDetector and script into it.

local function GetCash(plr)
	plr.PlayerStats.Coins.Value += 5
end

script.Parent.ClickDetector.MouseClick:Connect(GetCash)

Ok, now publish the game. Go to the game on the create tab, click configure experience then places, then Main Game. Finally play the game and it will work!!!

2 Likes

the leaderstats script didnt worked, also at the main game i have a system that counts how many minutes and seconds you played the game and they go by 2 leaderstats named “Minutes” and “Seconds”.

Check if you have API Access enabled (Game Settings, Security, API Access) and if you want to change the leaderstats to minutes and seconds then go ahead.

it is enabled, and i tried and didnt worked.

I seems to be working for me, here is the place file
TeleportWithData.rbxl (27.1 KB)

i fixed the problem, thanks for the help mate!

1 Like