So i’ve been searching the developers hub for remote functions, and I found two scripts, but I don’t know which one I need to use for my game.
I’m currently making something that when a player clicks a purchase button that a model gets cloned(I already know how I can clone that model). And for that I need remote functions, so no exploiter/hacker can give themself a lot of cash. But i’m not sure which script is most useful to use. server to client or client to server?
And after that I need to know how I can make it that when a player clicks the purchase button that it does something.
Can someone help me with this?
Here is the script from client to server;
-- LocalScript
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local createPartRequest = ReplicatedStorage:WaitForChild("CreatePartRequest")
local newPart = createPartRequest:InvokeServer()
print("The server created this part for me:", newPart)
-- ==================================================
-- Script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local createPartRequest = Instance.new("RemoteFunction")
createPartRequest.Parent = ReplicatedStorage
createPartRequest.Name = "CreatePartRequest"
local function onCreatePartRequested(player)
print(player.Name, "wants to create a new part")
local newPart = Instance.new("Part")
newPart.Parent = game.Workspace
return newPart
end
createPartRequest.OnServerInvoke = onCreatePartRequested
and here is the script server to client;
-- Server
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local createPopupRequest = Instance.new("RemoteFunction")
createPopupRequest.Name = "CreatePopupRequest"
createPopupRequest.Parent = ReplicatedStorage
Players.CharacterAutoLoads = false
local function onPlayerAdded(player)
createPopupRequest:InvokeClient(player)
player:LoadCharacter()
end
Players.PlayerAdded:Connect(onPlayerAdded)
-- ==================================================
-- LocalScript
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local createPopupRequest = ReplicatedStorage:WaitForChild("CreatePopupRequest")
local function onCreatePopupRequested()
local screen = Instance.new("ScreenGui")
screen.Parent = playerGui
local closeButton = Instance.new("TextButton")
closeButton.Text = "Welcome to the game! Click me to play!"
closeButton.Size = UDim2.new(0, 300, 0, 50)
closeButton.Parent = screen
closeButton.MouseButton1Click:Wait()
closeButton.Visible = false
end
createPopupRequest.OnClientInvoke = onCreatePopupRequested
wait(10) -- i put this in here so i can change the value to less or higher before it starts running
local player = game.Players.LocalPlayer
local Cash = player:WaitForChild("leaderstats"):WaitForChild("Cash")
local leaderstats = player.leaderstats
local hover = game.SoundService.HoverSound
if Cash.Value < 300 then -- checks if the cash is under 300
script.Parent.Parent.Text = "You don't have enough cash to buy this!" -- sets the text to this
script.Parent.Text = "Close"
else
script.Parent.Parent.Text = "This will cost: $300 Are you sure?"
end
script.Parent.MouseEnter:Connect(function()
hover:Play()
end)
script.Parent.MouseButton1Click:Connect(function(click)
wait(5)
if Cash.Value < 300 then
warn("You dont have enough cash")
script.Parent.Parent.Visible = false
else
script.Parent.Parent.Text = "This will cost: $300 Are you sure?"
script.Parent.Text = "Purchase"
script.Parent.Purchase.Disabled = true -- disables the script
print("PurchaseScript has been disabled") -- prints what happening
Cash.Value = Cash.Value - 300 -- sets the cash to min 300
print("leaderstats has been changed with -300")
script.Parent.Parent.Visible = false
script.Parent.Parent.Parent.BackGround.Philips.PriceSetter.Visible = true
script.Parent.Parent.Parent.BackGround.Philips.Selection.Visible = false
wait(5)
local philipsclone = game.ReplicatedStorage.Tvs.Philipstv1.PartsOfPhilips:Clone()
philipsclone.Parent = game.Workspace
local position = game.ReplicatedStorage.Tvs.Philipstv1.PartsOfPhilips.Screen
-- dit en de regel eronder is voor de positie waar hij word gecloned dus in de machine
for i, v in pairs(philipsclone:GetChildren()) do
v.Anchored = false
v.CanCollide = true
v.Transparency = 0
end
-- positions
local rotatepositionCFrame = CFrame.Angles(0,math.rad(0), 0)
position.CFrame = position.CFrame:ToWorldSpace(rotatepositionCFrame)
print("The CFrame has been set")
wait(10)
wait(30)
print("30 Seconds are over!")
script.Parent.Parent.Parent.BackGround.Philips.PriceSetter.Visible = false
script.Parent.Parent.Parent.BackGround.Philips.Selection.Visible = true
script.Disabled = false
print("PurchaseScript has been enabled")
game.StarterGui.ComputerMenu.Menu1.Philips1Info.PurchaseChecker.Purchase.Purchase.Disabled = false
end
end)
Now how should I put this into the other script for the remote function?