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

Teleport script doesn't work in server ?

Asked by
LeadRDRK 437 Moderation Voter
7 years ago

This teleport script that work normally in studio, but doesn't work at all in a server. Can someone fix this for me ?

01local mapIns = "map" .. script.Parent.Parent.mapID.Value
02local spawnL = workspace[mapIns].spawn
03local hitRequired = script.Parent.Parent.hitReq.Value
04 
05script.Parent.Touched:connect(function(hit)
06    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
07    local notice = player.PlayerGui.notice.text
08    local playerstats = player.leaderstats
09    if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") and playerstats.Hits.Value >= hitRequired then
10        hit.Parent.HumanoidRootPart.CFrame = CFrame.new(spawnL.Position)
11    elseif playerstats.Hits.Value < hitRequired then
12        notice.Text = "You need " .. hitRequired .. " hits or more to enter this!"
13        notice.Visible = true
14        wait(3.5)
15        notice.Visible = false
16    end
17end)

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

The server doesn't have access to the PlayerGui, but luckily the client has access to the Character. You need to do this from the client:

LocalScript in StarterPack or PlayerGui

01local plr = game.Players.LocalPlayer --You
02local SomeBrick --The brick to touch
03local mapIns = "map" .. SomeBrick.Parent.mapID.Value
04local spawnL = workspace[mapIns].spawn
05local hitRequired = SomeBrick.Parent.hitReq.Value
06local notice = plr:WaitForChild("PlayerGui"):WaitForChild("notice"):WaitForChild("text")
07local playerstats = plr:WaitForChild("leaderstats")
08 
09SomeBrick.Touched:Connect(function(hit)
10    --Get player that touched
11    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
12     --If it's you, and you have prerequisites
13    if player == plr and playerstats.Hits.Value >= hitRequired then
14        --Teleport
15        local root = hit.Parent.HumanoidRootPart
View all 24 lines...
You should look into adding a Debounce
0
It worked correctly now. Thanks! LeadRDRK 437 — 7y
Ad

Answer this question