ServerScriptService 脚本看不到玩家值的变化

问题描述 投票:0回答:1

我想为答题器创建一个我的宠物系统。我为玩家创造价值

ServerScriptService 中的主要脚本:

local genX=Instance.new("IntValue", player) --actual player multiply by pets
genX.Value=10
genX.Name="genPetX"

我有一个脚本,每次点击宠物时都会更改它(xClicks 包含每只宠物,意味着它将添加到点击中的 x)

Pet 中的本地脚本:

    local player= game.Players.LocalPlayer
local Equip=false

script.Parent.MouseButton1Click:Connect(function() 
    if Equip==false then
        player.genPetX.Value+=script.Parent.xClicks.Value
        Equip=true
    else
        player.genPetX.Value-=script.Parent.xClicks.Value
        Equip=false
    end
end)

我什至有每次 genPetX 更改时都会更改的标签并打印一些文本

标签中的本地脚本:

local localplayer=game.Players.LocalPlayer
localplayer.PlayerGui.ScreenGui.xPet.Text=localplayer.genPetX.Value

localplayer.genPetX.Changed:Connect(function ()
    localplayer.PlayerGui.ScreenGui.xPet.Text=localplayer.genPetX.Value
    game.ReplicatedStorage.getX:FireServer()
end)

ServerScriptService 中的主要脚本:

game.ReplicatedStorage.getX.OnServerEvent:Connect(function(player)
    print ("I watched you chaaaaangeeedddddd")
end)

但是当我单击时,即使标签说它已更改,它也会使用默认的 genPetX=10 值

ServerScriptService 中的主要脚本:

game.ReplicatedStorage.click.OnServerEvent:Connect(function(player)
  player.leaderstats.Clicks.Value+=player.ClicksPerClick.Value*player.genPetX.Value
end)

我不知道我需要改变什么才能让它工作,我尝试了一些改变,但正如你所读到的,它不起作用。

lua scripting roblox
1个回答
0
投票

您遇到了客户端-服务器复制的常见问题。当服务器更改某些内容(例如值或对象的位置)时,它会将更改复制到所有客户端。但是,出于安全原因,如果客户端更改了某些内容,则该更改不会复制到服务器或任何其他客户端。

所以你的代码的问题是服务器已经创建了 IntValue,

genPetX
,但是客户端是试图更改其值的人。因此,该值将在您的客户端上发生变化,但服务器永远不会看到该变化。但是您的解决方案是正确的,您需要使用 RemoteEvent 来请求服务器更改 IntValue 的值。

因此,在 ReplicatedStorage 中创建一个专门用于更新

genPetX
值的 RemoteEvent,将其命名为
setX
之类的名称,然后按照以下步骤操作...

所以在 ServerScriptService 的主脚本中:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- create the IntValues inside the Player when they join the game
Players.PlayerAdded:Connect(function(player)
    local genX = Instance.new("IntValue", player) --actual player multiply by pets
    genX.Value = 10
    genX.Name = "genPetX"
    -- create any other values here...
end)

-- listen for when a client signals that they have clicked on a pet
ReplicatedStorage.click.OnServerEvent:Connect(function(player)
    local cpc = player.ClicksPerClick.Value
    local gpx = player.genPetX.Value
    player.leaderstats.Clicks.Value += cpc * gpx
end)

-- listen for when a client tells you to update the `genPetX` value, the client will tell you the amount
ReplicatedStorage.setX.OnServerEvent:Connect(function(player, amount)
    assert(type(amount) == "number")
    player.genPetX.Value += amount
end)

然后,在标签的 LocalScript 中:

local localplayer = game.Players.LocalPlayer
local genPetX = localplayer.genPetX

local label = localplayer.PlayerGui.ScreenGui.xPet
label.Text = tostring(genPetX.Value)

-- keep the label constantly updated with changes to the IntValue's Value
genPetX.Changed:Connect(function(newValue)
    label.Text = tostring(newValue)
end)

最后,在您宠物的 LocalScript 中,使用 RemoteEvent 请求服务器更新您的值:(我假设这是在工具中)

local player = game.Players.LocalPlayer
local setX = game.ReplicatedStorage.setX
local xClicks = script.Parent.xClicks

local equip = false

script.Parent.MouseButton1Click:Connect(function()
    -- when equipping, subtract xClicks otherwise add them
    local amount = if equip and -xClicks.Value else xClicks.Value

    -- tell the server to update the player's genPetX value by the amount specified
    -- NOTE : THIS IS INHERENTLY UNSAFE, DON'T TRUST THE CLIENT TO EVER TELL THE TRUTH
    setX.FireServer(amount)

    -- toggle the value for equip
    equip = not equip
end)
© www.soinside.com 2019 - 2024. All rights reserved.