Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Change Instance (Integer) that was made by a script using another?

Asked by
ZenTGE 4
6 years ago
Edited 6 years ago

I created an instance using another script. It was data storage. I created an integer, which were credits. I'm making a shop that reduces your credit amount when you buy a weapon. Since the instance was created with the earlier script, is there some way I can talk to that script using the shop button one?

game.players.PlayerAdded:Connect(function(player)
    game.Workspace.DataStore 
-- This is as far as I have gotten, and the only piece that is important. Are lines of code children of the script, "Datastore" or how do you talk to "leaderstats" or, "credits?"
end)

0
Please post your code. It helps a lot. User#19524 175 — 6y
0
Edited, not much, but that is why I posted here. ZenTGE 4 — 6y
0
Will your shop be physical (a building obviously) or a GUI? User#19524 175 — 6y
0
GUI. A Dialog GUI. It brings the shop up. I am trying to connect the buttons to reduce your credits,and clone the weapon from replicatedstorage. ZenTGE 4 — 6y
0
I created the credits USING datastorage. How do I talk to that with the button script to further reduce the integer? ZenTGE 4 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

This will be a LocalScript under your TextButton or ImageButton. I will be using RemoteEvents and RemoteFunctions, as ROBLOX may be removing experimental mode. Learn more on that here.

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local BuyRemotes = ReplicatedStorage:WaitForChild("BuyWeaponRemotes")
--^ BuyWeaponRemotes is a Folder in ReplicatedStorage.
local CheckPrice = BuyRemotes:WaitForChild("CheckPrice")
local CloneTool = BuyRemotes:WaitForChild("CloneTool")
local Price = CheckPrice:InvokeServer("Sword") 
-- ^We will be returning the price here, and "Sword" just change to weapon name.
local player = Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function() 
    -- Listens for the button being clicked

    CloneTool:FireServer("Sword", Price)
end)

Server code, recommend to put it in ServerScriptService.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local BuyRemotes = ReplicatedStorage:WaitForChild("BuyWeaponRemotes")
local CheckPrice = BuyRemotes:WaitForChild("CheckPrice")
local Weapons = ReplicatedStorage:WaitForChild("Weapons")
--^ Another folder inside replicatedstorage. Put your weapon in here.
local CloneTool = BuyRemotes:WaitForChild("CloneTool")
--[[ You may notice we don't have LocalPlayer. 
    Do not use it on the server, only in LocalScripts.
--]]

local GetPrice = function(player, toolName)
    return Weapons[toolName].Price.Value
    -- Make sure an IntValue named price is inside the tool.
end

CheckPrice.OnServerInvoke = GetPrice

CloneTool.OnServerEvent:Connect(function(player, toolName, price)
        --[[
            up to you check if the player have more or enough,
            and the weapon cloning.
        --]]
    end
end)

I won't give the full code. Though there's a basic understanding on how. Here's a few things that you should read.

https://devforum.roblox.com/t/changes-to-playing-ability-of-experimental-mode-games/146494.

http://robloxdev.com/articles/Remote-Functions-and-Events

0
I have yet to test it, and I had not know about remote events. Thank you. I will test it soon. ZenTGE 4 — 6y
0
Ok. By the way, CheckPrice is a RemoteFunction, whilst CloneTool is a RemoteEvent User#19524 175 — 6y
0
Also, why do you need to get replicated storage? It's already game.ReplicatedStorage. ZenTGE 4 — 6y
Ad

Answer this question