Zwykły Event Touched dzięki któremu obiekt zmieni kolor po dotknięciu
-- Event z odwołaniem do funkcji
local barrel = script.Parent
local function OnTouch(other)
barrel.BrickColor = BrickColor.random()
end
barrel.Touched:Connect(OnTouch)
-- Event z funkcją anonimową
local barrel = script.Parent
barrel.Touched:Connect(function()
barrel.BrickColor = BrickColor.random()
end)
Event, który wywoła funkcję tylko w przypadku humanoidów lub playera
local barrel = script.Parent
local function OnTouch(other)
--local hum = other.Parent:FindFirstChild("Humanoid")
local player = game.Players:GetPlayerFromCharacter(other.Parent)
if player then
barrel.BrickColor = BrickColor.random()
end
end
barrel.Touched:Connect(OnTouch)
Remote Event, który łączy różne obiekty
Przykład zbierania monet w Roblox z wyświetleniem komunikatu textowego gui
Stwórz RemoteEvent w ReplicatedStorage o nazwie CollectCoinEvent
ReplicatedStorage jest widoczny zarówno dla serwera jak i klienta
--collectable script w obiekcie monety w workspace
local remoteEvent = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local item = script.Parent
local function OnTouch(other)
local player = game.Players:GetPlayerFromCharacter(other.Parent)
if player then
remoteEvent:FireClient(player, "+1")
item:Destroy()
end
end
item.Touched:Connect(OnTouch)
-- Gui local script na obiekcie Gui w StarterGui
-- Gui script
local collectCoinEvent = game.ReplicatedStorage:WaitForChild("CollectCoinEvent")
local label = script.Parent
local tween = game.TweenService
label.Visible = false
function DisplayCollectGUI(msg)
label.Visible = true
label.Position = UDim2.fromScale(0.5, 0.5)
label.Text = msg
local tweenAnim = tween:Create(
label,
TweenInfo.new(2),
{Position=UDim2.new(.5,0,-.5,0)}
)
tweenAnim:Play()
end
-- OnClientEvent can be used ONLY in LocalScript or module script
collectCoinEvent.OnClientEvent:Connect(DisplayCollectGUI)
Tworzenie leaderstats
--gamemanager in ServiceScriptService
local function AddBoard(player)
local board = Instance.new("Folder", player)
board.Name = "leaderstats"
local coins = Instance.new("IntValue", board)
coins.Name = "Coins"
end
game.Players.PlayerAdded:Connect(AddBoard)
coins
local coin = script.Parent
local remoteEvent = game.ReplicatedStorage.RemoteEvent
coin.CanCollide = false
local function OnTouch(other)
local player = game.Players:GetPlayerFromCharacter(other.Parent)
if player then
local coins = player.leaderstats["Coins"]
coins.Value += 1
print("touched")
remoteEvent:FireClient(player, "+1")
coin:Destroy()
end
end
coin.Touched:Connect(OnTouch)
Data stores
W ustawieniach Security wlacz Enable Studio Access to API Services
local dss = game:GetService("DataStoreService")
local ds = dss:GetDataStore("MyData")
local function InitializePlayerData(player)
local data = nil
local success, msg = pcall(function()
data = ds:GetAsync(player.UserId)
end)
if not success then
player:Kick("data not avail", msg)
end
if data then
player.leaderstats.Coins.Value = data.Coins
else
data = {Coins=0}
end
ds:SetAsync(player.UserId, data)
end
local function SavePlayerData(player)
local data = {}
data.Coins = player.leaderstats.Coins.Value
ds:SetAsync(player.UserId, data)
end
local function SaveLoop()
while wait(5) do
local players = game.Players:GetChildren()
for i, player in pairs(players) do
local board = player:FindFirstChild("leaderstats")
if board then
SavePlayerData(player)
wait()
end
end
end
end
local function AddBoard(player)
local board = Instance.new("Folder", player)
board.Name = "leaderstats"
local coins = Instance.new("IntValue", board)
coins.Name = "Coins"
InitializePlayerData(player)
end
spawn(SaveLoop)
game.Players.PlayerAdded:Connect(AddBoard)
game.Players.PlayerRemoving:Connect(SavePlayerData)