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

While Loop not functioning as intended, why?

Asked by
BryanFehr 133
4 years ago

Hey y'all, my server script inside a part in the workspace is supposed to check if the player has a specific leaderstat value, in this case, that value's name is Door1, and if it's greater than or equal to the required_points variable.

I have a GUI that pops up On touch of the part, which works properly, if the player doesn't have the required_points value. And the Buy button script works properly, and it changes the value (+1 from current value). As well, the ServerScript inside of the part in the Workspace works properly as well if the player ALREADY HAS the specific value.

What I am asking, is why when the player buys the area(buying the area just changes their leaderstat Value which is Door1), the script won't detect that the players value has been changed, and make the Parts CanCollide false for that player.

Any and all help is appreciated! Scripts posted Below!

SERVER SCRIPT

local required_points = 2
local db = true
while true do
    wait(1.5)
        script.Parent.Touched:Connect(function(hit)
        if hit.Parent:FindFirstChild("Humanoid") then
            local player = game.Players:GetPlayerFromCharacter(hit.Parent)
            if player.leaderstats.Door1.Value >= required_points then
                if db then
                    db = false
                    script.Parent.CanCollide = false
                    db = true
                end
            else
                if player and not player.PlayerGui:FindFirstChild("buydoor") then
                local cloneui = script.buydoor:Clone()
                cloneui.Parent = player.PlayerGui
                cloneui.Frame:TweenPosition(UDim2.new(0.25,0,0.2,0))
wait(1) 
                end
            end
        end
    end)
end

LOCAL SCRIPT INSIDE TEXTBUTTON IN GUI

local player = game.Players.LocalPlayer
local leaderstats = player:WaitForChild("leaderstats")

script.Parent.MouseButton1Click:Connect(function()
    if player.leaderstats.BrickBux.Value > 1 then
    wait(2)
    script.Parent.Text = "Purchased!"
    player.leaderstats.Door1.Value = player.leaderstats.Door1.Value + 1
    player.leaderstats.BrickBux.Value = player.leaderstats.BrickBux.Value - 150
    wait(1)
    script.Parent.Parent.Parent.Frame:TweenPosition(UDim2.new(-0.5, 0,0.3, 0))
    wait(1)
    player.PlayerGui.buydoor:Destroy()
    else
        script.Parent.BackgroundColor3 = Color3.fromRGB(255,25,25)
        script.Parent.TextColor3 = Color3.fromRGB(255,0,0)
        script.Parent.Text = "Failed!"
        wait(1.5)
        script.Parent.Parent.Parent.Frame:TweenPosition(UDim2.new(-0.5, 0,0.3, 0))
        wait(1)
        script.Parent.Parent.Parent:Destroy()
    end
end)
1
Changing ValueObjects from a LocalScript does not replicate to the server, since the introduction of FilteringEnabled over 2 years ago. I suspect whatever code you're using as an example is very outdated; you have to do client-to-server communication using RemoteEvents now. EmilyBendsSpace 1025 — 4y
1
Also, setting a part to CanCollide false on the server sets it for all players. Anyone will be able to walk through the part. Per-player collision control is best done through CollisionGroups. EmilyBendsSpace 1025 — 4y
0
I don't think you would need that "else" on line 14 in the server script 0msh 333 — 4y

Answer this question