Question about coins system in my roblox game

Hello, I’m rip_Zephyr, and I’m making a game involving coins. However, I want to be able to access the coins variable from a different script, such as when you step on a part. If anyone can tell me how then I would appreciate it. Otherwise I can lie on my bed for a few hours figuring out how to do it myself. This is my current code for the actual coins system.

local players = game:GetService("Players")
local dataStores = game:GetService("DataStoreService")
local playerCoinsDataStore = dataStores:GetDataStore("PlayerCoins")

players.PlayerAdded:Connect(function(plr)
	
	PlayerCoins = Instance.new('IntValue')
	PlayerCoins.Name = "PlayerCoins"
	PlayerCoins.Parent = plr
	
	task.wait(1)
	
	PlayerCoins.Value = 0
	
	local coinsData = playerCoinsDataStore:GetAsync(plr.UserId)
	
	if coinsData then
		print("Player has data!")
		PlayerCoins.Value = coinsData	
	else
		print("Player has no data... womp womp")
		playerCoinsDataStore:SetAsync(plr.UserID, 0)
		print("Player now has data... yipee!!!")
	end
end)

players.PlayerRemoving:Connect(function(plr)
	local PlayerCoins = plr:WaitForChild("PlayerCoins")
	playerCoinsDataStore:SetAsync(plr.UserId, PlayerCoins.Value)
end)
2 Likes

You can use a BindableEvent to communicate between different scripts.

We can insert a BindableEvent inside your current coins system script. Then from another script we can fire that BindableEvent like this:

Other script:

local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local Workspace = game:GetService("Workspace")

local coinsSystemScript = ServerScriptService.CoinsSystem -- change it do what you named the coin system script
local coinSystemBindableEvent = coinsSystemScript.BindableEvent


-- example for touching part
local part = Workspace.Part
part.Touched:Connect(function(otherPart: BasePart)
    local character  = otherPart.Parent
    local player = character and Players:GetPlayerFromCharacter(character)
    if character and player then
        coinSystemBindableEvent:Fire(player)
    end
end)

Original Coins System Script:

local players = game:GetService("Players")
local dataStores = game:GetService("DataStoreService")
local playerCoinsDataStore = dataStores:GetDataStore("PlayerCoins")

local coinSystemBindableEvent = script.BindableEvent

coinSystemBindableEvent.Event:Connect(function(player: Player)
	local playerCoins = player:FindFirstChild("PlayerCoins")
	if not playerCoins then
		return
	end
	playerCoins.Value += 10
end)

players.PlayerAdded:Connect(function(plr)

	PlayerCoins = Instance.new('IntValue')
	PlayerCoins.Name = "PlayerCoins"
	PlayerCoins.Parent = plr

	task.wait(1)

	PlayerCoins.Value = 0

	local coinsData = playerCoinsDataStore:GetAsync(plr.UserId)

	if coinsData then
		print("Player has data!")
		PlayerCoins.Value = coinsData	
	else
		print("Player has no data... womp womp")
		playerCoinsDataStore:SetAsync(plr.UserID, 0)
		print("Player now has data... yipee!!!")
	end
end)

players.PlayerRemoving:Connect(function(plr)
	local PlayerCoins = plr:WaitForChild("PlayerCoins")
	playerCoinsDataStore:SetAsync(plr.UserId, PlayerCoins.Value)
end)

Just access the value from the player object? (Player.PlayerCoins.Value)

Here’s an example:

local Players = game:GetService("Players")

local part = script.Parent -- The part the player steps on

part.Touched:Connect(function(hit)
	local character = hit.Parent
	local player = Players:GetPlayerFromCharacter(character)
	if player then
		local PlayerCoins = player:FindFirstChild("PlayerCoins")
		if PlayerCoins then
			PlayerCoins.Value = PlayerCoins.Value + 10 -- Add 10 coins
			print(player.Name .. " now has " .. PlayerCoins.Value .. " coins.")
		end
	end
end)

This is very unnecessary, @StellarisJames’ solution would be better.

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